【问题标题】:Model binding when rendering a partial view multiple times (partial view used for editing)多次渲染局部视图时的模型绑定(用于编辑的局部视图)
【发布时间】:2010-11-25 02:40:57
【问题描述】:

大家好,

我在将部分视图用作编辑表单时遇到了一点问题。除了表单中的隐藏元素之外,模型在传递给我的 EditContact 后控制器时为空。

我应该补充一点,我意识到使用 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>

当 Edit contact post 控制器被传递时,模型只有 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 模型。

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

当然,我可能会离开这里,一般来说需要一个新的解决方案! 如果您需要更多详细信息,请告诉我!谢谢!

【问题讨论】:

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


    【解决方案1】:

    你为什么要使用

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

    如果你像这样简单地使用它

    &lt;%= Html.EditorFor(Model =&gt; Model.salutation) %&gt;

    它应该可以工作。

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

    【讨论】:

    • 嗯...有趣!那行得通。我会将其标记为已回答。我正在重命名 ID,以便它们是唯一的。例如,当输入新联系人但用户取消时,我需要能够清除联系人部分。联系人有一个类型,并且地址视图中还有另一个部分也有一个类型。两个 id 在 html 中具有相同的名称。我会多玩一点,看看我能不能把它弄出来。再次感谢。
    【解决方案2】:

    使用 Html.EditorFor 代替 Html.RenderPartial。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多