在MVC 3中回发到控制器操作后,ViewModel集合属性丢失了值

时间:2012-03-10 13:04:43

标签: asp.net-mvc asp.net-mvc-3 razor 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&lt;&gt;我可以按照自己的意愿坚持查看模型数据。但不在列表&lt;&gt;导航属性。

@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中。

提前致谢!

3 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:0)

您丢失了列表,因为MVC没有按照您的想法处理List。

您应该使用BeginCollectionItem查看this post

答案 2 :(得分:0)

我有一个类似的问题,没有值(count = 0)返回的“List”属性,我尝试了不同的方法和答案,并且都没有工作。 然后我自己尝试,现在它正在工作,这是我的解决方案:

我发送了一个带有一些普通属性和一个“List”的对象,之后我在For中使用了普通属性和我的“列表”。 在我的控制器(Post ActionResult)中,在参数部分我添加了两个参数,我的原始对象和我的“List”作为第二个参数,它的工作原理! 我希望这可以帮助你和其他有类似问题的人。