MVC 中的绑定集合
我有一个由 Applicant
对象和 TeamMember
集合组成的视图模型。当我将模型发回时,团队集合始终为空。我尝试将集合从原来的 IEnumarable
更改为 List
但这并没有什么区别。因此,我更改了控制器编辑操作以接受 FormCollection
,并验证 viewModel["member.FirstName"]
中是否有数据。我不明白为什么绑定不起作用。我尝试尽可能多地清理我的代码示例,但我对缺少的内容感到困惑。非常感谢任何帮助!
查看模型属性
public class MyViewModel
{
public Applicant ApplicantInfo { get; set; }
public List<TeamMember> TeamMembers { get; set; }
}
控制器
[HttpPost]
public ActionResult Edit(MyViewModel viewModel)
{
// viewModel.ApplicantInfo has the form data
// viewModel.TeamMembers = null
}
视图
<% using (Html.BeginForm())
{%>
<h3>
<a href="#">Applicant Information</a>
</h3>
<label>
City
<%: Html.TextBoxFor(m => Model.ApplicantInfo.City)%>
</label>
<label>
State
<%: Html.TextBoxFor(m => Model.ApplicantInfo.State)%>
</label>
<h3>
<a href="#">Team</a>
</h3>
<div>
<% foreach (var member in Model.TeamMembers)
{ %>
<div class="editor-field">
<%: Html.DropDownList("member.Type", Model.GetMemberTypes(member.MemberType.TypeId))%>
</div>
<div class="editor-field">
<%: Html.EditorFor(m => member.FirstName)%>
</div>
<div class="editor-field">
<%: Html.EditorFor(m => member.LastName)%>
</div>
<div class="editor-field">
<%: Html.EditorFor(m => member.Title)%>
</div>
<%} %>
</div>
<p>
<input type="submit" value="Save" />
</p>
<% } %>
I have a View Model that consists of an Applicant
object and a TeamMember
collection. When I post the model back the Team collection is always null. I've tried changing the collection from my original IEnumarable
to a List
but that didn't make a difference. So I changed the Controllers Edit Action to accept the FormCollection
, and verified there was data in viewModel["member.FirstName"]
. I'm lost as to why the binding isn't working. I tried to clean out my code samples as much as possible but I'm confused at what I'm missing. Any help is greatly appreciated!
View Model Properties
public class MyViewModel
{
public Applicant ApplicantInfo { get; set; }
public List<TeamMember> TeamMembers { get; set; }
}
Controller
[HttpPost]
public ActionResult Edit(MyViewModel viewModel)
{
// viewModel.ApplicantInfo has the form data
// viewModel.TeamMembers = null
}
View
<% using (Html.BeginForm())
{%>
<h3>
<a href="#">Applicant Information</a>
</h3>
<label>
City
<%: Html.TextBoxFor(m => Model.ApplicantInfo.City)%>
</label>
<label>
State
<%: Html.TextBoxFor(m => Model.ApplicantInfo.State)%>
</label>
<h3>
<a href="#">Team</a>
</h3>
<div>
<% foreach (var member in Model.TeamMembers)
{ %>
<div class="editor-field">
<%: Html.DropDownList("member.Type", Model.GetMemberTypes(member.MemberType.TypeId))%>
</div>
<div class="editor-field">
<%: Html.EditorFor(m => member.FirstName)%>
</div>
<div class="editor-field">
<%: Html.EditorFor(m => member.LastName)%>
</div>
<div class="editor-field">
<%: Html.EditorFor(m => member.Title)%>
</div>
<%} %>
</div>
<p>
<input type="submit" value="Save" />
</p>
<% } %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信与集合中的项目关联的输入标签(当模型本身不是集合时)需要在名称属性中具有索引,然后才能将发布的数据绑定到视图模型。这是我通常完成此操作的方法...
我也使用了 Shea 建议的模板,但我有更多代码试图强制它呈现括号/索引。
这是一篇旧但仍然相关的文章 来自 Phil Haack 的主题。
I believe that input tags associated with items in a collection (when the model itself is not a collection) need to have an index in the name attribute before you can bind posted data to a view model. Here is the way I usually accomplish this...
I've also used the template as suggested by Shea, but I have a tad more code trying to force it to render brackets/indexes.
Here is an old but still relevant article from Phil Haack on the topic.
尝试使用以下方法:
然后,创建一个模型类型为
TeamMember
的共享编辑器模板。 MVC 应该为您处理将所有内容绑定回您的视图模型。Try using this:
Then, make a shared editor template with a model type of
TeamMember
. MVC should handle binding everything back to your viewmodel on post for you.