MVC 中的绑定集合

发布于 2024-10-16 16:18:22 字数 1966 浏览 2 评论 0原文

我有一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失退 2024-10-23 16:18:22

我相信与集合中的项目关联的输入标签(当模型本身不是集合​​时)需要在名称属性中具有索引,然后才能将发布的数据绑定到视图模型。这是我通常完成此操作的方法...

<% for (int i=0; i<Model.TeamMembers.Count; i++) { %>
<div class="editor-field">
  <%: Html.EditorFor(m => m.TeamMembers[i].FirstName)%>
</div>
<div class="editor-field">
  <%: Html.EditorFor(m => m.TeamMembers[i].LastName)%>
</div>
<% } %>

我也使用了 Shea 建议的模板,但我有更多代码试图强制它呈现括号/索引。

<% foreach (var member in Model.TeamMembers) { %>
  <%: Html.EditorFor(x => 
    member, 
    "TeamMember", 
    "TeamMembers["+(member.Number-1)+"]", 
    new { MemberTypes = Model.GetMemberTypes(member.MemberType.TypeId) })%>
<% } %>

这是一篇旧但仍然相关的文章 来自 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...

<% for (int i=0; i<Model.TeamMembers.Count; i++) { %>
<div class="editor-field">
  <%: Html.EditorFor(m => m.TeamMembers[i].FirstName)%>
</div>
<div class="editor-field">
  <%: Html.EditorFor(m => m.TeamMembers[i].LastName)%>
</div>
<% } %>

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.

<% foreach (var member in Model.TeamMembers) { %>
  <%: Html.EditorFor(x => 
    member, 
    "TeamMember", 
    "TeamMembers["+(member.Number-1)+"]", 
    new { MemberTypes = Model.GetMemberTypes(member.MemberType.TypeId) })%>
<% } %>

Here is an old but still relevant article from Phil Haack on the topic.

飞烟轻若梦 2024-10-23 16:18:22

尝试使用以下方法:

<%: Html.EditorFor(m => m.TeamMembers) %>

然后,创建一个模型类型为 TeamMember 的共享编辑器模板。 MVC 应该为您处理将所有内容绑定回您的视图模型。

Try using this:

<%: Html.EditorFor(m => m.TeamMembers) %>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文