How do I clear the bind on these properties so that changes I'm trying to make in an onpost method pull through?
I have 6 bound properties in a ShipmentModel:
[BindProperty(SupportsGet = true)] public ShipmentModel Shipment { get; set; }
public class ShipmentModel
{
public string CurrentShipDt1 { get; set; }
public int CurrentShipQty1 { get; set; }
public string CurrentShipDt2 { get; set; }
public int CurrentShipQty2 { get; set; }
public string CurrentShipDt3 { get; set; }
public int CurrentShipQty3 { get; set; }
}
In my onget, I run a few linq queries and it properly posts the results to those properties:
public async Task<IActionResult> OnGetAsync(string partNo, string requestStatus, string supplier, string searchString)
{
CmtPartShipSchedule = await _context.CmtPartShipSchedules
.OrderByDescending(m => m.EnterDt)
.FirstOrDefaultAsync(m => m.PartNo == partNo);
if (CmtPartShipSchedule != null){
var shipDtQuery = (from c in _context.CmtPartShipments
where CmtPartShipSchedule.Id == c.CmtPartShipScheduleId
orderby c.ShipDt descending
select c.ShipDt).ToList();
List<DateTime> CmtPartShipDts = shipDtQuery;
var shipQtyQuery = (from c in _context.CmtPartShipments
where CmtPartShipSchedule.Id == c.CmtPartShipScheduleId
orderby c.ShipDt descending
select c.ShipQty).ToList();
List<int> CmtPartShipQtys = shipQtyQuery;
CmtPartShipment = await _context.CmtPartShipments
.FirstOrDefaultAsync(m => m.CmtPartShipScheduleId == CmtPartShipSchedule.Id);
int shipCount = shipDtQuery.Count();
if (shipCount > 0)
{
if (CmtPartShipDts[0] != null & CmtPartShipQtys[0] != 0)
{
string ShipDt1 = CmtPartShipDts[0].ToString("yyyy-MM-dd");
Shipment.CurrentShipDt1 = ShipDt1;
Shipment.CurrentShipQty1 = CmtPartShipQtys[0];
}
}
if (shipCount > 1)
{
if (CmtPartShipDts[1] != null & CmtPartShipQtys[1] != 0)
{
string ShipDt2 = CmtPartShipDts[1].ToString("yyyy-MM-dd");
Shipment.CurrentShipDt2 = ShipDt2;
Shipment.CurrentShipQty2 = CmtPartShipQtys[1];
}
}
if (shipCount > 2)
{
if (CmtPartShipDts[2] != null & CmtPartShipQtys[2] != 0)
{
string ShipDt3 = CmtPartShipDts[2].ToString("yyyy-MM-dd");
Shipment.CurrentShipDt3 = ShipDt3;
Shipment.CurrentShipQty3 = CmtPartShipQtys[2];
}
}
}
return Page();
}
The problem is with an onpost method I've created - It should clear out any properties that have values in them that were set in my onget at the click of a button. However, I can't get it to do just that.. At this point, it properly runs through my method but doesn't post the results (because of the model bind which is overwriting these changes I'm trying to make, I assume). Here's what I have:
public async Task<IActionResult> OnPostCurrentPOsAsync(string partNo, string requestStatus, string supplier, string searchString)
{
if (!ModelState.IsValid) { return Page(); }
Shipment.CurrentShipDt1 = null; Shipment.CurrentShipQty1 = 0;
Shipment.CurrentShipDt2 = null; Shipment.CurrentShipQty2 = 0;
Shipment.CurrentShipDt3 = null; Shipment.CurrentShipQty3 = 0;
int shipCount = POETAs.Count();
if (shipCount > 0)
{
if (POETAs[0] != null & POQtys[0] != 0)
{
string ShipDt1 = POETAs[0].ToString("yyyy-MM-dd");
Shipment.CurrentShipDt1 = ShipDt1;
Shipment.CurrentShipQty1 = POQtys[0];
}
}
if (shipCount > 1)
{
if (POETAs[1] != null & POQtys[1] != 0)
{
string ShipDt2 = POETAs[1].ToString("yyyy-MM-dd");
Shipment.CurrentShipDt2 = ShipDt2;
Shipment.CurrentShipQty2 = POQtys[1];
}
}
if (shipCount > 2)
{
if (POETAs[2] != null & POQtys[2] != 0)
{
string ShipDt3 = POETAs[2].ToString("yyyy-MM-dd");
Shipment.CurrentShipDt3 = ShipDt3;
Shipment.CurrentShipQty3 = POQtys[2];
}
}
CurrentSupplier = supplier;
return RedirectToPage("", new { partNo = CurrentPart, requestStatus = RequestStatus, supplier = CurrentSupplier, searchString = SearchString });
}
What I have tried:
Originally I was using return Page(); and a
similar post here told me to change that to return RedirectToPage(), still no change. Also, referencing
this article, I tried using ModelState.Remove to "clear the bind", still no change.