gpt4 book ai didi

asp.net-mvc - ViewModel 集合属性在 MVC 3 中回发到 Controller 操作后丢失值

转载 作者:行者123 更新时间:2023-12-02 08:39:59 25 4
gpt4 key购买 nike

我有我的 View 模型:

public class POReceiptViewModel
{
public virtual int PONumber { get; set; }
public virtual string VendorCode { get; set; }

public virtual IList<POReceiptItemViewModel> POReceiptItems { get; set; }

public POReceiptViewModel()
{
POReceiptItems = new List<POReceiptItemViewModel>();
}
}

public class POReceiptItemViewModel
{
public virtual string ItemCode { get; set; }
public virtual string ItemDesription { get; set; }
public virtual decimal OrderedQuantity { get; set; }
public virtual decimal ReceivedQuantity { get; set; }
public virtual DateTime ReceivedDate { get; set; }

public POReceiptItemViewModel()
{
ReceivedDate = DateTime.Now;
}
}

然后我的 Controller 有两个操作,一个 get 和一个 post:

public ActionResult CreatePOReceipt(int poNumber)
{
PurchaseOrder po = PurchasingService.GetPurchaseOrder(poNumber);
POReceiptViewModel poReceiptViewModel = ModelBuilder.POToPOReceiptViewModel(po);
return View("CreatePOReceipt", poReceiptViewModel);
}

[HttpPost]
public ActionResult CreatePOReceipt(POReceiptViewModel poReceiptViewModel)
{
// Here the problem goes. The items in the poReceiptViewModel.POReceiptItems has lost. the count became zero.
return View("Index");
}

在我的 View 中,我可以正确显示模型,并且通过使用 @Html.HiddenFor<> 我可以根据需要保留 View 模型数据。但不在 List<> 导航属性上。

@model POReceiptViewModel

@using (Html.BeginForm())
{
<fieldset>
<legend>Purchase Order</legend>
<label>For PO # :</label>
@Html.HiddenFor(m => m.PONumber)
@Html.DisplayTextFor(m => m.PONumber)
<label>Vendor Code :</label>
@Html.HiddenFor(m => m.VendorCode)
@Html.DisplayTextFor(m => m.VendorCode)
</fieldset>

<fieldset>
<legend>Received Items</legend>

<table class="tbl" id="tbl">
<thead>
<tr>
<th>Item Code</th><th>Item Description</th><th>OrderedQuantity</th><th>Received Quantity</th><th>Received Date</th>
</tr>
</thead>
<tbody>
@Html.HiddenFor(m => m.POReceiptItems) // I'm not really sure if this is valid
@if (Model.POReceiptItems.Count > 0)
{
foreach (var item in Model.POReceiptItems)
{
<tr>
<td>@Html.DisplayTextFor(i => item.ItemCode)</td>@Html.HiddenFor(i => item.ItemCode)
<td>@Html.DisplayTextFor(i => item.ItemDesription)</td>@Html.HiddenFor(i => item.ItemDesription)
<td>@Html.DisplayTextFor(i => item.OrderedQuantity)</td>@Html.HiddenFor(i => item.OrderedQuantity)
<td>@Html.TextBoxFor(i => item.ReceivedQuantity)</td>
<td>@Html.TextBoxFor(i => item.ReceivedDate)</td>
</tr>
}
}
</tbody>
</table>
</fieldset>
<input type="submit" name="Received" value="Received" />
}

问题:提交表单时,POReceiptItems 丢失。我尽可能不想使用 TempData["POReceiptItems"] = Model.POReceiptItems 但即使我使用它,输入到 ReceivedQuantity 和 ReceivedDate 中的值也不会保存到 TempData 中。

提前致谢!

最佳答案

尝试

@for (int i = 0; i < Model.POReceiptItems.Count(); i++)
{
<tr>
<td>@Html.DisplayTextFor(m => m.POReceiptItems[i].ItemCode)</td>@Html.HiddenFor(m => m.POReceiptItems[i].ItemCode)
<td>@Html.DisplayTextFor(m => m.POReceiptItems[i].ItemDesription)</td>@Html.HiddenFor(m => m.POReceiptItems.ItemDesription) <td>@Html.DisplayTextFor(m => m.POReceiptItems[i].OrderedQuantity)</td>@Html.HiddenFor(m => m.POReceiptItems[i].OrderedQuantity)
<td>@Html.TextBoxFor(m => m.POReceiptItems[i].ReceivedQuantity)</td>
<td>@Html.TextBoxFor(m => m.POReceiptItems[i].ReceivedDate)</td>
</tr>
}

另请阅读此blog post了解模型绑定(bind)到列表的工作原理

关于asp.net-mvc - ViewModel 集合属性在 MVC 3 中回发到 Controller 操作后丢失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9646656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com