I have model
public class UserModel : IUserModel
{
public LocationModel WorkLocation { get; set; }
public LocationModel HomeLocation { get; set; }
public LocationModel ShippingLocation { get; set; }
}
I created partial view what show fields from LocationModel class.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<LocationModel>" %>
<fieldset>
<div class="common-fieldset">
<div>
<span class="editor-label">
<%: Html.LabelFor(model => model.Location.Bldg)%>
</span><span class="editor-field">
<%: Html.EditorFor(model => model.Location.Bldg)%>
</span><span class="editor-label">
<%: Html.LabelFor(model => model.Location.Lobby)%>
</span><span class="editor-field">
<%: Html.EditorFor(model => model.Location.Lobby)%>
</span><span class="editor-label">
<%: Html.LabelFor(model => model.Location.Suite)%>
</span><span class="editor-field">
<%: Html.EditorFor(model => model.Location.Suite)%>
</span>
</div>
</div>
</fieldset>
When controller render my UserModel evening looks good.
But all fields IDs in HTML from my LocationModel classes have the same name, and on post back LocationModel properties contain not init values. It render each of this property without partial view all working fine. Why ? Thanks.
<div>
Home Address:
<%: Html.Partial("Controls/Location", Model.HomeLocation) %>
</div>
<div>
Work Address:
<%: Html.Partial("Controls/Location", Model.WorkLocation) %>
</div>
<div> Shipping Address:
<%: Html.Partial("Controls/Location",Model.ShippingLocation) %>
</div>
Instead of using a partial I would recommend you using an editor template. So move this
LocationModel.ascxinside~/Views/Shared/EditorTemplates/LocationModel.ascxand then simply:Now all your inputs will have correct ids and names.