MVC - 部分视图中的字段需要唯一ID。你怎么做到这一点?

时间:2010-09-14 13:29:24

标签: asp.net-mvc

在我看来,我在循环中渲染局部视图。 我遇到的问题是,对于每个新行,字段的Id保持不变。 我可以改变它,以便Ids是唯一且可预测的吗?

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.BankHolidayViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    BankHoliday
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">
    <% using (Html.BeginForm())
       {%>
        <h3>Bank Holiday Administration</h3>
        <p>Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%></p>
            <fieldset>
            <legend>Enter the bank holidays here:</legend>
            <table>
                <tr>
                    <th>Bank Holiday</th>
                    <th>Date</th>
                    <th>Notes</th>
                </tr>
                <% foreach (var bankHolidayExtended in Model.BankHolidays)
                   { %>
                    <% Html.RenderPartial("BankHolidaySummary", bankHolidayExtended); %>
                <% } %>
               <tr>
                    <td align="center" colspan="3" style="padding-top:20px;">
                        <input type="submit" value="Create" />
                    </td>
                </tr>
            </table>
            </fieldset>
        <% } %>

部分视图

“%&gt;
    <tr>
        <td><%: Model.T.BankHolidayDescription%></td>
        <td><%: Html.EditorFor(model => model.BH.NullableBankHolidayDate)%></td>
        <td><%: Html.EditorFor(model => model.BH.BankHolidayComment)%></td>
    </tr>

1 个答案:

答案 0 :(得分:4)

我建议您使用editor templates而不是渲染部分视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.BankHolidayViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    BankHoliday
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">
    <% using (Html.BeginForm()) { %>
        <h3>Bank Holiday Administration</h3>
        <p>Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%></p>
        <fieldset>
            <legend>Enter the bank holidays here:</legend>
            <table>
                <tr>
                    <th>Bank Holiday</th>
                    <th>Date</th>
                    <th>Notes</th>
                </tr>
                <%: Html.EditorFor(x => x.BankHolidays) %>
                <tr>
                    <td align="center" colspan="3" style="padding-top:20px;">
                        <input type="submit" value="Create" />
                    </td>
                </tr>
            </table>
        </fieldset>
    <% } %>
</asp:Content>

然后将您的用户控件放在~/Views/Home/EditorTemplates/BankHolidayExtended.ascx中(名称和位置很重要,将Home替换为您的控制器名称):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.WebUI.Models.BankHolidayExtended>" %>
<tr>
    <td><%: Model.T.BankHolidayDescription %></td>
    <td><%: Html.EditorFor(model => model.BH.NullableBankHolidayDate) %></td>
    <td><%: Html.EditorFor(model => model.BH.BankHolidayComment) %></td>
</tr>

将为视图模型上的BankHolidays集合属性的每个元素自动呈现此编辑器模板。它将确保生成唯一的ID,并且输入字段的名称将适合在POST操作中进行绑定。它还使您的代码更具可读性。