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.
Darin Dimitrov
source share