Reputation: 14098
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>
Upvotes: 1
Views: 668
Reputation: 1039348
I created partial view what show fields from LocationModel class.
Instead of using a partial I would recommend you using an editor template. So move this LocationModel.ascx
inside ~/Views/Shared/EditorTemplates/LocationModel.ascx
and then simply:
<div>
Home Address:
<%= Html.EditorFor(x => x.HomeLocation) %>
</div>
<div>
Work Address:
<%= Html.EditorFor(x => x.WorkLocation) %>
</div>
<div>
Shipping Address:
<%= Html.EditorFor(x => x.ShippingLocation) %>
</div>
Now all your inputs will have correct ids and names.
Upvotes: 1