发布时清除绑定属性

如何清除这些属性上的绑定,以便我尝试在 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();


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

我尝试过的:最初我使用的是 return Page(); 这里的类似帖子告诉我将其更改为返回 RedirectToPage(),但仍然没有任何变化。另外,参考这篇文章,我尝试使用 ModelState.Remove 来“清除绑定”,仍然没有变化。



小唯快跑啊
浏览 101回答 1
1回答

侃侃无极

这有效(我在修改值之前清除模型状态,而不是之后!):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();
打开App,查看更多内容
随时随地看视频慕课网APP