多次渲染局部视图时的模型绑定(用于编辑的局部视图)

时间:2010-10-16 00:10:27

标签: asp.net-mvc asp.net-mvc-2

问候每个人,

我在使用部分视图时遇到了一些问题,我正在使用它作为编辑表单。除了表单中隐藏的元素,模型在传递给我的EditContact帖子控制器时为null。

我应该补充一点,我意识到有一种更无缝的方式来做我正在使用AJAX做的事情,但我想如果我能先解决笨重的方式,它会帮助我更好地理解这项工作。 这是我的EditContact局部视图:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WebUI.DataAccess.CONTACT>" %>
<div id="contactPartial-<%= ViewData.Model.id %>">
    <% Html.EnableClientValidation(); %>
    <% using (Html.BeginForm("EditContact", "Investigator"))
       { %>
    <%= Html.HiddenFor(Model => Model.id) %>
    <%= Html.HiddenFor(Model => Model.investId) %>
    <table id="EditContact-<%= ViewData.Model.id %>" width="100%">
        <tr>
            <th colspan="4">
                <b>New Contact</b>
            </th>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.type)%></b><br />
                <%= Html.EditorFor(Model => Model.type, null, "contactType-" + ViewData.Model.id)%><br />
                <%= Html.ValidationMessageFor(Model => Model.type) %>
            </td>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.salutation)%></b><br />
                <%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.firstName)%></b><br />
                <%= Html.EditorFor(Model => Model.firstName, null, "contactFirstName-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.middleName)%></b><br />
                <%= Html.EditorFor(Model => Model.middleName, null, "contactMiddleName-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.lastName)%></b><br />
                <%= Html.EditorFor(Model => Model.lastName, null, "contactLastName-"+ViewData.Model.id)%><br />
                <%= Html.ValidationMessageFor(Model => Model.lastName)%>
            </td>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.title)%></b><br />
                <%= Html.EditorFor(Model => Model.title, null, "contactTitle-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.phone)%></b><br />
                <%= Html.EditorFor(Model => Model.phone, null, "contactPhone="+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.altPhone)%></b><br />
                <%= Html.EditorFor(Model => Model.altPhone, null, "contactAltPhone-" + ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.fax)%></b><br />
                <%= Html.EditorFor(Model => Model.fax, null, "contactFax-" + ViewData.Model.id)%>
            </td>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.email)%></b><br />
                <%= Html.EditorFor(Model => Model.email, null, "contactEmail-" + ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.comment)%></b><br />
                <%= Html.EditorFor(Model => Model.comment, null, "contactComment-" + ViewData.Model.id)%>
            </td>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Save" />
            </td>
            <td>
                <button id="<%= ViewData.Model.id %>" class="contactEditHide">
                    Cancel</button>
            </td>
            <td>
            </td>
            <td>
            </td>
        </tr>
    </table>
    <% } %>
</div>

当传递编辑联系人帖子控制器时,仅模型id和investId具有值;其他一切都是空的。这是我在详细信息视图中渲染局部视图的方式:

   <table width="100%">
    <tr>
        <th colspan="7">
            <b>Contacts</b>
        </th>
        <th id="contactButton" class="button">
            <button id="showEditContact-0" type="button" class="contactEditShow">New Contact</button>
        </th>
    </tr>
    <tr>
        <th>
            Type
        </th>
        <th>
            Name
        </th>
        <th colspan="2">
            Title
        </th>
        <th>
            Prim. & Alt. Phone
        </th>
        <th>
            Fax
        </th>
        <th colspan="2">
            Comment
        </th>
    </tr>
    <% for (int i = 0; i < Model.CONTACTs.Count; i++)
       { %>           
    <tr id="contactRow-<%= Model.CONTACTs[i].id %>" class="contactRow">
        <td>
            <%= Html.Encode(Model.CONTACTs[i].type)%>
        </td>
        <td>
            <%= Html.Encode(Model.CONTACTs[i].salutation)%>
            <%= Html.Encode(Model.CONTACTs[i].firstName)%>
            <%= Html.Encode(Model.CONTACTs[i].middleName)%>
            <%= Html.Encode(Model.CONTACTs[i].lastName)%>
            <br />
            <a href="mailto:<%= Html.AttributeEncode(Model.CONTACTs[i].email) %>">
                <%= Html.Encode(Model.CONTACTs[i].email)%></a>
        </td>
        <td colspan="2">
            <%= Html.Encode(Model.CONTACTs[i].title)%>
        </td>
        <td>
            Prim:<%= Html.Encode(Model.CONTACTs[i].phone)%><br />
            Alt:<%= Html.Encode(Model.CONTACTs[i].altPhone)%>
        </td>
        <td>
            <%= Html.Encode(Model.CONTACTs[i].fax)%>
        </td>
        <td>
            <%= Html.Encode(Model.CONTACTs[i].comment)%>
        </td>
        <td>
            <button id="<%= Model.CONTACTs[i].id %>" type="button" class="contactEditShow">Edit Contact</button>
        </td>
    </tr>
    <tr>
        <td colspan="8">
            <% Html.RenderPartial("EditContact", Model.CONTACTs[i]); %>
        </td>
    </tr>
    <% } %>
</table>

我的详细信息视图强烈关注INVESTIGATOR模型:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.DataAccess.INVESTIGATOR>" %>

如上所示,我的部分视图是对CONTACTS模型的强类型。

我在模型绑定中遇到了类似的问题,在一个视图中,我试图允许一次编辑地址的所有层并通过索引模型来修复它,类似于上面的Details视图。看来我需要在部分视图中做同样的事情以确保模型绑定,但我不确定如何实现这一点(有语法问题)。

当然,我可能会在这里需要一个新的解决方案! 如果您需要更多细节,请告诉我们!谢谢!

2 个答案:

答案 0 :(得分:4)

你为什么使用

<%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>

如果您使用它就像这样

<%= Html.EditorFor(Model => Model.salutation) %>

应该有效。

为了映射您的模型,您的控件需要具有与其名称相同的ID,并且您正在使用的第三个参数会更改您的控件ID。

答案 1 :(得分:1)

使用Html.EditorFor代替Html.RenderPartial。

请参阅Model binding with nested child models and PartialViews in ASP.NET MVC