我有一个在编辑页面中使用局部视图的 ASP MVC3 项目。它将正确的值加载到 中DropDownListFor
,但是在 POST BACK 中,这些项目突然为空。
该视图使用视图模型来合并几个 SQL 表。作为该视图模型的一部分,List
类型的集合对象BankListAgentId
用于存储与特定客户端相关的所有代理 ID。为了正确发布和编辑集合对象中的项目,我关注了Steve Sanderson关于 ASP MVC 中的可变长度对象的博客文章。
这个过程非常适合代理 ID,但是一旦用户点击“保存”,状态代码就会全部为空。
这是视图中的代码
@model Monet.ViewModel.BankListViewModel
@using (Html.BeginForm())
{
<fieldset>
<legend>Stat(s) Fixed</legend>
<table id="fixedRows">
<tr>
<th>State Code</th>
<th>Agent ID</th>
<th></th>
</tr>
@foreach (var item in Model.Fixed)
{
if (!String.IsNullOrWhiteSpace(item.AgentId))
{
@Html.Partial("FixedPartialView", item)
}
}
</table>
<br />
@Html.ActionLink("Add another", "BlankFixedRow", null, null, new { id = "addFixed" })
</fieldset>
}
这是部分
@model Monet.Models.BankListAgentId
@using (Html.BeginCollectionItem("Variable"))
{
<tr>
<td>
@Html.DropDownListFor(model => model.StateCode,
(SelectList)ViewBag.StateCodeList, Model.StateCode)
</td>
<td>
@Html.EditorFor(model => model.AgentId)
@Html.ValidationMessageFor(model => model.AgentId)
</td>
<td>
<a href="#" onclick="$(this).parent().remove();" style="float:right;">Delete</a></td>
</tr>
}
这是视图模型
public class BankListViewModel
{
public int ID { get; set; }
public string BankName { get; set; }
public string LastChangeOperator { get; set; }
public Nullable<System.DateTime> LastChangeDate { 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());
}
}
这是从控制器到编辑页面的原始 POST
public ActionResult Edit(int id)
{
BankListMaster banklistmaster = db.BankListMaster.Find(id);
BankListViewModel viewModel = new BankListViewModel();
viewModel.BankName = banklistmaster.BankName;
viewModel.LastChangeDate = banklistmaster.LastChangeDate;
viewModel.LastChangeOperator = banklistmaster.LastChangeOperator;
List<BankListAgentId> agentId = (from c in db.BankListAgentId
where c.ID == id
select c).ToList();
foreach (var bankListAgentId in agentId)
{
string value = bankListAgentId.FixedOrVariable.Trim();
if (value.Equals("Fixed"))
{
viewModel.Fixed.Add(bankListAgentId);
}
else
{
viewModel.Variable.Add(bankListAgentId);
}
}
viewModel.Attachments = (from c in db.BankListAttachments
where c.ID == id
select c).ToList();
SelectList tmpList = new SelectList(new[] { "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NA", "NM", "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "US", "VT", "VI", "VA", "WA", "WV", "WI", "WY" });
ViewBag.StateCodeList = tmpList;
ViewBag.ID = new SelectList(db.BankListAgentId, "ID", "FixedOrVariable", banklistmaster.ID);
return View(viewModel);
}
public partial class BankListAgentId
{
public string AgentId { get; set; }
public int ID { get; set; }
public string FixedOrVariable { get; set; }
public string StateCode { get; set; }
public virtual BankListMaster BankListMaster { get; set; }
}