Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear bound properties when posting

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)
{
    // Find the current shipment info for this part
    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(); }

    // Tried to clear the bind so that I can overwrite.. Doesn't change anything
    // https://weblog.west-wind.com/posts/2012/apr/20/aspnet-mvc-postbacks-and-htmlhelper-controls-ignoring-model-changes
    //ModelState.Remove(Shipment.CurrentShipDt1);
    //ModelState.Remove(Shipment.CurrentShipDt2);
    //ModelState.Remove(Shipment.CurrentShipDt3);
    //ModelState.Remove(Shipment.CurrentShipQty1.ToString());
    //ModelState.Remove(Shipment.CurrentShipQty2.ToString());
    //ModelState.Remove(Shipment.CurrentShipQty3.ToString());

    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've 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.

like image 673
mstiver2018 Avatar asked Dec 12 '25 15:12

mstiver2018


1 Answers

Posting an answer per Mike Brind's comment. This worked (I was clearing the model state BEFORE my amended values, not after!):

Shipment.CurrentShipDt1 = null; Shipment.CurrentShipQty1 = 0;
Shipment.CurrentShipDt2 = null; Shipment.CurrentShipQty2 = 0;
Shipment.CurrentShipDt3 = null; Shipment.CurrentShipQty3 = 0;
Shipment.CurrentShipDt4 = null; Shipment.CurrentShipQty4 = 0;
Shipment.CurrentShipDt5 = null; Shipment.CurrentShipQty5 = 0;

ModelState.Clear();

return Page();
like image 77
mstiver2018 Avatar answered Dec 15 '25 05:12

mstiver2018



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!