MVC - fields in a partial view require a unique identifier. How do you do this?

In my opinion, I am doing a partial view in a loop. The problem is that for each new row, the Field ID remains unchanged. Can I change this so that the identifiers 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

"%>
  <tr> <td><%: Model.T.BankHolidayDescription%></td> <td><%: Html.EditorFor(model => model.BH.NullableBankHolidayDate)%></td> <td><%: Html.EditorFor(model => model.BH.BankHolidayComment)%></td> </tr> 
+3
asp.net-mvc
source share
1 answer

I would recommend you use editor templates instead of partial viewing:

 <%@ 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 (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 displayed for each element of the BankHolidays collection BankHolidays in your view model. It will ensure the creation of unique identifiers and the name of the input fields will be suitable for binding in the POST action. It also makes your code more readable.

+4
source share

All Articles