MVC - 在部分领域视图需要一个唯一的ID。你怎么做到这一点? [英] MVC - fields in a partial View need a Unique Id. How do you do this?

查看:101
本文介绍了MVC - 在部分领域视图需要一个唯一的ID。你怎么做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我渲染循环中的局部视图。
我的问题是,对于每一个新的行,该字段的Id保持相同。
我我可以改变这一点,以便在ID是唯一和predictable?

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/ AdminAccounts.master继承=System.Web.Mvc.ViewPage< SHP.WebUI.Models.BankHolidayViewModel>中%GT;< ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器>
    BankHoliday
< / ASP:内容>< ASP:内容ID =内容2ContentPlaceHolderID =AdminAccountsContent=服务器>
    <使用%(Html.BeginForm())
       {%GT;
        < H3>银行假日管理< / H3 GT&;
        < P>选择年:<%:Html.DropDownListFor(型号=> model.SelectedYear,Model.YearList)%GT;< / P>
            <&字段集GT;
            <传奇>进入这里的银行假日:LT; /传说>
            <表>
                &所述; TR>
                    <第i个银行假日< /第i
                    <第i日期和LT; /第i
                    <第i注释和LT; /第i
                < / TR>
                <%的foreach(在Model.BankHolidays VAR bankHolidayExtended)
                   {%GT;
                    <%Html.RenderPartial(BankHolidaySummary,bankHolidayExtended); %GT;
                <%}%GT;
               &所述; TR>
                    < TD ALIGN =中心合并单元格=3的风格=填充顶:20像素;>
                        <输入类型=提交值=创建/>
                    < / TD>
                < / TR>
            < /表>
            < /字段集>
        <%}%GT;


管窥

<%@控制语言=C#继承=System.Web.Mvc.ViewUserControl%>

 < TR>
        &所述; TD>&下;%:Model.T.BankHolidayDescription%GT;&下; / TD>
        < TD><%:Html.EditorFor(型号=> model.BH.NullableBankHolidayDate)%GT;< / TD>
        < TD><%:Html.EditorFor(型号=> model.BH.BankHolidayComment)%GT;< / TD>
    < / TR>


解决方案

我在使用的编辑模板代替渲染局部视图:

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/ AdminAccounts.master继承=System.Web.Mvc.ViewPage< SHP.WebUI.Models.BankHolidayViewModel>中%GT;< ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器>
    BankHoliday
< / ASP:内容>< ASP:内容ID =内容2ContentPlaceHolderID =AdminAccountsContent=服务器>
    &下;使用%(Html.BeginForm()){%GT;
        < H3>银行假日管理< / H3 GT&;
        < P>选择年:<%:Html.DropDownListFor(型号=> model.SelectedYear,Model.YearList)%GT;< / P>
        <&字段集GT;
            <传奇>进入这里的银行假日:LT; /传说>
            <表>
                &所述; TR>
                    <第i个银行假日< /第i
                    <第i日期和LT; /第i
                    <第i注释和LT; /第i
                < / TR>
                <%:Html.EditorFor(X => x.BankHolidays)%GT;
                &所述; TR>
                    < TD ALIGN =中心合并单元格=3的风格=填充顶:20像素;>
                        <输入类型=提交值=创建/>
                    < / TD>
                < / TR>
            < /表>
        < /字段集>
    <%}%GT;
< / ASP:内容>

,然后将在用户控件〜/查看/主页/ EditorTemplates / BankHolidayExtended.ascx (名称和位置都很重要,替换首页与控制器的名称):

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl< SHP.WebUI.Models.BankHolidayExtended>中%GT;
&所述; TR>
    &所述; TD>&下;%:Model.T.BankHolidayDescription%GT;&下; / TD>
    < TD><%:Html.EditorFor(型号=> model.BH.NullableBankHolidayDate)%GT;< / TD>
    < TD><%:Html.EditorFor(型号=> model.BH.BankHolidayComment)%GT;< / TD>
< / TR>

这个编辑器模板将自动呈现为 BankHolidays 集合属性上的视图模型中的每个元素。这将保证唯一ID生成和输入字段的名称将是适合在POST操作绑定。这也使得你的code更具可读性。

In my view I render a partial view within a loop. The problem I have is that for each new row, the Id of the fields remains the same. I can I change this so that the Ids are unique and predictable?

<%@ 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>
        <% } %>


Partial View

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

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

解决方案

I would recommend you using editor templates instead of rendering partial views:

<%@ 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>

And then place your user control in ~/Views/Home/EditorTemplates/BankHolidayExtended.ascx (the name and location are important, replace Home with the name of your controller):

<%@ 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>

This editor template will be automatically rendered for each element of the BankHolidays collection property on your view model. It will ensure that unique Ids are generated and the name of the input fields will be suitable for binding in the POST action. It also makes your code more readable.

这篇关于MVC - 在部分领域视图需要一个唯一的ID。你怎么做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆