Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
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:

C#
[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:

C#
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:

C#
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 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.
Posted
Updated 22-Aug-19 4:31am

You need to pass property name to ModelState.Remove method, not the value.
Either pass the property name in string, eg. ModelState.Remove("CurrentShipDt1"); or in the newer .NET framework, you can use nameof() keyword, eg. ModelState.Remove(nameof(Shipment.CurrentShipDt1));
 
Share this answer
 
Comments
Member 14164795 22-Aug-19 10:32am    
This did not work for me, however I found my problem :) I was trying to remove the model state before setting my values to what I wanted, not after!
This worked for me:
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900