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