Asp.net mvc 编辑页面上的DropDownListFor在回发时将空值传递给控制器
我有一个ASP MVC3项目,它在编辑页面中使用部分视图。它将正确的值加载到Asp.net mvc 编辑页面上的DropDownListFor在回发时将空值传递给控制器,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-2,postback,html.dropdownlistfor,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 2,Postback,Html.dropdownlistfor,我有一个ASP MVC3项目,它在编辑页面中使用部分视图。它将正确的值加载到DropDownListFor中,但是在回发上,项目突然为空 @model Monet.Models.BankListAgentId @using (Html.BeginCollectionItem("Variable")) { <tr> <td> @Html.DropDownListFor(model => model.StateCode
DropDownListFor
中,但是在回发上,项目突然为空
@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>
}
视图使用视图模型来合并两个SQL表。作为该视图模型的一部分,使用类型为BankListAgentId的列表收集对象来存储与特定客户端相关的所有代理Id。为了正确地发布和编辑集合对象中的项,我遵循了ASP MVC中关于可变长度对象的说明
@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>
}
这个过程对于代理Id非常有效,但是一旦用户点击“保存”,状态代码都是空的
@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>
}
下面是视图中的代码
@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>
}
@model Monet.ViewModel.BankListViewModel
@使用(Html.BeginForm())
{
统计数据已修复
州代码
代理ID
@foreach(模型中的var项。已修复)
{
如果(!String.IsNullOrWhiteSpace(item.AgentId))
{
@Html.Partial(“FixedPartialView”,项目)
}
}
@ActionLink(“添加另一个”、“BlankFixedRow”、null、null、new{id=“addFixed”})
}
这是部分原因
@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>
}
@model Monet.Models.BankListAgentId
@使用(Html.BeginCollectionItem(“变量”))
{
@Html.DropDownListFor(model=>model.StateCode,
(选择列表)ViewBag.StateCodeList,Model.StateCode)
@EditorFor(model=>model.AgentId)
@Html.ValidationMessageFor(model=>model.AgentId)
}
这是视图模型
@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());
}
}
公共类BankListViewModel
{
公共int ID{get;set;}
公共字符串BankName{get;set;}
公共字符串LastChangeOperator{get;set;}
公共可为空的LastChangeDate{get;set;}
公共列表修复了{get;set;}
公共列表变量{get;set;}
公共列表附件{get;set;}
public BankListViewModel()
{
//初始化固定和可变统计列表
固定=新列表();
变量=新列表();
Models.BankListAgentId agentId=新的BankListAgentId();
对于(int i=0;i<5;i++)
{
固定。添加(agentId);
变量.Add(agentId);
}
//初始化附件列表
附件=新列表();
附件。添加(新的BankListAttachments());
}
}
这是从控制器发送到编辑页面的原始帖子
@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 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; }
}
public ActionResult编辑(int-id)
{
BankListMaster BankListMaster=db.BankListMaster.Find(id);
BankListViewModel viewModel=新的BankListViewModel();
viewModel.BankName=banklistmaster.BankName;
viewModel.LastChangeDate=banklistmaster.LastChangeDate;
viewModel.LastChangeOperator=banklistmaster.LastChangeOperator;
List agentId=(从db.BankListAgentId中的c开始)
其中c.ID==ID
选择c.ToList();
foreach(agentId中的var bankListAgentId)
{
字符串值=bankListAgentId.FixedOrVariable.Trim();
如果(值等于(“固定”))
{
viewModel.Fixed.Add(bankListAgentId);
}
其他的
{
viewModel.Variable.Add(bankListAgentId);
}
}
viewModel.Attachments=(来自db.BankListAttachments中的c)
其中c.ID==ID
选择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”、“NY”、“NC”、“ND”、“MP”、“OH”、“OK”、“OR”、“PW”、“PA”、“PR”、“RI”、“SC”、“SD”、“TN”","德克萨斯","德克萨斯","德克萨斯","美国","悉尼","弗吉尼亚",;
ViewBag.StateCodeList=tmpList;
ViewBag.ID=新选择列表(db.BankListAgentId,“ID”,“FixedOrVariable”,banklistmaster.ID);
返回视图(viewModel);
}
公共部分类BankListAgentId
{
公共字符串AgentId{get;set;}
公共int ID{get;set;}
公共字符串固定变量{get;set;}
公共字符串状态码{get;set;}
公共虚拟银行列表主机银行列表主机{get;set;}
}
问题在于,您使用的是类似于EditorTemplate的局部视图。局部视图和编辑器模板是不同的。您可以将局部视图视为服务器端包含一些额外内容
@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>
}
因此,这里的问题是,您向partial传递的模型与在父类中使用的不同
@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>
}
@foreach (var item in Model.Fixed)
{
if (!String.IsNullOrWhiteSpace(item.AgentId))
{
@Html.Partial("FixedPartialView", item)
}
}
提交模型时,局部视图中的帮助程序将代码呈现为模型已修复,因此它会错误地格式化和命名局部视图中的项目。在这种情况下,默认模型绑定器不知道“item”在一个名为“Fixed”的集合中,因为您已经通过迭代foreach循环并仅从包含的项中呈现来删除该集合的上下文
@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>
}
我建议不要使用局部视图,而是使用EditorTemplate,因为这是设计用来处理的(对象中的渲染类型),并且它们会自动处理集合
@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>
}
或者,如果希望渲染局部,则不要传入子模型,只让它从父模型渲染。否则,您将不得不做大量工作,以确保在分部中有正确的命名
@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>
}
或者,如果只需要发布此项目集合,则可以确保发布操作接受IEnumerable
,而不是父mo