本文介绍了在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;
    }
}

然后我的控制器执行两项操作,一次执行并执行一项操作:

Then my controller has two actions, one get and one 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");
}

在我的视图中,我可以正确显示模型,并且可以使用@ Html.HiddenFor<>持久保存视图模型数据.但不在List<>导航属性上.

And in my View, I can display the model properly and by using @Html.HiddenFor<> I can persist view model data as I wanted to. But not on the List<> navigation property.

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

PROBLEM:POReceiptItems lost when the form submitted. As much as possible I don't want to use TempData["POReceiptItems"] = Model.POReceiptItems but even if I use it, the value entered into ReceivedQuantity and ReceivedDate are not save into the 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>
}

还请阅读此 博客文章 了解如何将模型绑定到列表上

also read this blog post to understand how model binding to a list works

这篇关于在MVC 3中发回控制器操作后,ViewModel集合属性丢失了值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 20:34