ASP MVC3视图模型在回发时将数据从集合对象丢失到编辑方法

时间:2013-04-02 20:40:23

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 postback viewmodel

我的ASP MVC3视图模型在Edit方法加载此特定页面时工作正常,但是当用户点击保存并进行回发时,两个集合对象(List<T>)中包含的所有信息都将丢失。有任何想法吗?

以下是视图中的代码。这会正确加载所有代理信息(ID和状态代码)

    @for (int i = 0; i < Model.Fixed.Count; i++)
    {           

        if(!String.IsNullOrWhiteSpace(Model.Fixed[i].AgentId))
        {
           fixedRow++;

           if (fixedRow > 2)
           {
               var rowId = "row" + fixedRow.ToString() + "F";
               <tr id=rowId class="noSee">
                    <td>
                        @Html.DropDownListFor(model => model.Fixed[i].StateCode,
                         (SelectList)ViewBag.StateCodeList, Model.Fixed[i].StateCode)
                    </td>
                    <td>
                        @Html.EditorFor(model => model.Fixed[i].AgentId)
                        @Html.ValidationMessageFor(model => model.Fixed[i].AgentId)
                    </td>
                    @if(fixedRow > 1)
                    {
                        var send = "MoreFixed(" + (fixedRow + 1).ToString() + ");";
                        var dataId = "plus" + fixedRow.ToString() + "F;";
                        <td id=@dataId  class="more" onclick=@send>+</td>
                    }
               </tr> 
           }
           else
           {
                <tr>
                    <td>
                        @Html.DropDownListFor(model => model.Fixed[i].StateCode,
                         (SelectList)ViewBag.StateCodeList, Model.Fixed[i].StateCode)
                    </td>
                    <td>
                        @Html.EditorFor(model => model.Fixed[i].AgentId)
                        @Html.ValidationMessageFor(model => model.Fixed[i].AgentId)
                    </td>
                    @if(fixedRow > 1)
                    {
                        var send = "MoreFixed(" + (fixedRow + 1).ToString() + ");";
                        var id = "plus" + fixedRow.ToString() + "F;";
                        <td id=@id class="more" onclick=@send>+</td>
                    }

                </tr> 
           }        
        }
    }

以下是视图模型中的代码

public class BankListViewModel
{
    public int ID { get; set; }
    public string BankName { get; set; }
    public string Tier { get; set; }
    public string SpecialNotes { get; set; }
    public string WelcomLetterReq { get; set; }

    public List<BankListAgentId> Fixed { get; set; }
    public List<BankListAgentId> Variable { get; set; }
    public List<BankListAttachments> Attachments { get; set; }

    public BankListViewModel()
    {
        //Initialize Fixed and Variable stat Lists
        Fixed = new List<BankListAgentId>();
        Variable = new List<BankListAgentId>();

        Models.BankListAgentId agentId = new BankListAgentId();

        for (int i = 0; i < 5; i++)
        {
            Fixed.Add(agentId);
            Variable.Add(agentId);                
        }

        //Initialize attachment Lists
        Attachments = new List<BankListAttachments>();
        Attachments.Add(new BankListAttachments());
    }
}

这是接收回发的控制器方法。再次,当页面加载时, GET Edit方法从数据库中收集所有正确的信息,并将视图模型正确地返回给视图。

    [HttpPost]
    public ActionResult Edit(BankListViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            List<BankListAgentId> agentId = new List<BankListAgentId>();
            List<BankListAttachments> attachments = new List<BankListAttachments>();
            BankListMaster master = new BankListMaster();

            master = decipherViewModel(viewModel, out agentId, out attachments);

            db.Entry(master).State = EntityState.Modified;
            db.SaveChanges();


            return RedirectToAction("Index");
        }
        ViewBag.ID = new SelectList(db.BankListAgentId, "ID", "FixedOrVariable", viewModel.ID);
        return View(viewModel);
    } 

    private BankListMaster decipherViewModel(BankListViewModel viewModel, out List<BankListAgentId> agentId, out List<BankListAttachments> attachments)    
    {
        //Initialize
        BankListMaster banklistmaster = new BankListMaster();
        agentId = new List<BankListAgentId>();
        attachments = new List<BankListAttachments>();

        viewModel.BankName = banklistmaster.BankName;
        viewModel.SpecialNotes = banklistmaster.SpecialNotes;
        viewModel.Tier = banklistmaster.Tier;

        foreach (var item in viewModel.Fixed)
        {
            item.ID = viewModel.ID;
            agentId.Add(item);
        }

        foreach (var item in viewModel.Variable)
        {
            item.ID = viewModel.ID;
            agentId.Add(item); 
        }

        foreach (var item in viewModel.Attachments)
        {
            item.ID = viewModel.ID;
            attachments.Add(item);
        }

        return banklistmaster;
    }

2 个答案:

答案 0 :(得分:8)

回发到控制器的唯一内容是Html表单字段字段。而已。如果您不将信息放在表单字段中(输入,选择,文本区域等),那么它将不会被回发。

如果您希望将数据发回,那么您必须将其放在隐藏字段中。在回发中简单地在数据库中查看数据可能更容易。

答案 1 :(得分:1)

这些值不会被提交回控制器。提交的内容是使用集合中的值从视图中创建的下拉列表中进行选择,前提是下拉列表位于您提交的表单中。