我有模特
public class UserModel : IUserModel
{
public LocationModel WorkLocation { get; set; }
public LocationModel HomeLocation { get; set; }
public LocationModel ShippingLocation { get; set; }
}
我创建了部分视图来显示LocationModel类的字段。
<%@ 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>
当控制器渲染我的UserModel晚上看起来不错。 但是,我的LocationModel类中的HTML中的所有字段ID都具有相同的名称,并且在post post上,LocationModel属性不包含init值。它渲染每个属性而没有局部视图都可以正常工作。为什么?感谢。
<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>
答案 0 :(得分:1)
我创建了部分视图来显示LocationModel类的字段。
我建议您使用编辑器模板而不是部分使用。因此,将此LocationModel.ascx
移到~/Views/Shared/EditorTemplates/LocationModel.ascx
内,然后简单地:
<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>
现在所有输入都有正确的ID和名称。