发布时清除绑定属性
Clear bound properties when posting
如何清除对这些属性的绑定,以便我尝试在 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; }
}
在我开始时,我 运行 一些 linq 查询,它正确地 post 将结果发送到这些属性:
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 中设置的。但是,我无法让它做到这一点。此时,它正确地 运行s 通过我的方法但没有 post 结果(因为模型绑定正在覆盖这些我假设我正在尝试进行更改)。这是我拥有的:
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();和类似的 told me to change that to return RedirectToPage(), still no change. Also, referencing this article,我尝试使用 ModelState.Remove 到 "clear the bind",仍然没有变化。
根据 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();
如何清除对这些属性的绑定,以便我尝试在 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; }
}
在我开始时,我 运行 一些 linq 查询,它正确地 post 将结果发送到这些属性:
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 中设置的。但是,我无法让它做到这一点。此时,它正确地 运行s 通过我的方法但没有 post 结果(因为模型绑定正在覆盖这些我假设我正在尝试进行更改)。这是我拥有的:
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();和类似的
根据 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();