2

我的 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());
    }
}

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

    [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;
    }
4

2 回答 2

8

唯一返回到控制器的是 Html 表单字段字段。就是这样。如果您不将信息放在表单域(输入、选择、文本区域等)中,则不会回发。

如果您想要回发该数据,则必须将其放置在隐藏字段中。在回发时简单地在数据库中查找该数据可能更容易。

于 2013-04-02T21:01:51.730 回答
1

这些值不会提交回控制器。提交的是您在视图中使用集合中的值创建的下拉列表中的选择,因为下拉列表位于您重新提交的表单中。

于 2013-04-02T20:53:22.013 回答