【问题标题】:Clear bound properties when posting发布时清除绑定属性
【发布时间】:2019-08-22 13:10:01
【问题描述】:

如何清除这些属性的绑定,以便我尝试在 onpost 方法中进行的更改通过?

我在 ShipmentModel 中有 6 个绑定属性:

[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; }
}

在我的 onget 中,我运行了一些 linq 查询并将结果正确地发布到这些属性:

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();
}

问题出在我创建的 onpost 方法上 - 它应该清除所有具有值的属性,这些属性是通过单击按钮在我的 onget 中设置的。但是,我不能让它这样做.. 在这一点上,它正确地运行了我的方法,但没有发布结果(因为模型绑定覆盖了我正在尝试进行的这些更改,我认为)。这是我所拥有的:

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 });
}

我尝试过的: 最初我使用的是 return Page();和一个类似的post here 告诉我改变它以返回 RedirectToPage(),仍然没有改变。另外,参考this article,我尝试使用 ModelState.Remove 来“清除绑定”,仍然没有变化。

【问题讨论】:

  • 修改发布的值,调用 ModelState.Clear()return Page() 对我来说很好。表单将重新填充修改后的值,而不是发布的值。这就是你想要达到的目标吗?
  • @MikeBrind 一如既往的有用!当我尝试 ModelState.Clear() 时,我的问题是我把它放在修改值之前,而不是之后!谢谢。

标签: c# asp.net-core model-binding razor-pages


【解决方案1】:

根据 Mike Brind 的评论发布答案。这有效(我在修改值之前清除模型状态,而不是之后!):

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();

【讨论】:

    猜你喜欢
    • 2019-02-03
    • 2023-03-25
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多