asp.net-mvc – 在MVC 3中回发到控制器操作后,ViewModel集合属性丢失了值

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 在MVC 3中回发到控制器操作后,ViewModel集合属性丢失了值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有我的视图模型:
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;
    }
}

然后我的控制器有两个动作,一个获得一个帖子:

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

在我的视图中,我可以正确显示模型并使用@ Html.HiddenFor<>我可以按照自己的意愿坚持查看模型数据.但不在列表<>导航属性.

@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以了解模型绑定到列表的工作原理

原文链接:https://www.f2er.com/aspnet/248654.html

猜你在找的asp.Net相关文章