一页上相同的部分视图,如何处理绑定?
我对 MVC 还是个新手。我目前有一个添加人员页面,该页面包括人员的名字、姓氏和家庭地址,这是一个复杂的对象(包括地址行1、地址行2、州等)、送货地址(与家庭地址相同)。
所以我想创建一个用于显示地址的部分视图,但是当我提交表单时,我无法区分哪个值是家庭地址,因为家庭地址和送货地址在表单上呈现相同的名称。
MyClass
Public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int DateOfBirthMonth { get; set; }
public Gender Sex { get; set; }
public AddressModel HomeAddress { get; set; }
public AddressModel DeliveryAddress { get; set; }
public Person()
{
HomeAddress = new AddressModel();
DeliveryAddress = new AddressModel();
}
}
public class AddressModel
{
public string Addr1 { get; set; }
public string Addr2 { get; set; }
public string Suburb { get; set; }
}
public enum Gender
{
Male = 1,
Female = 2
}
我的地址 部分视图
@model MvcApplication1.Models.AddressModel
<fieldset>
<legend>Address</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Addr1)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Addr1)
@Html.ValidationMessageFor(model => model.Addr1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Addr2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Addr2)
@Html.ValidationMessageFor(model => model.Addr2)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Suburb)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Suburb)
@Html.ValidationMessageFor(model => model.Suburb)
</div>
添加人员页面
@using MvcApplication1.Models
@model MvcApplication1.Models.Person
@{
ViewBag.Title = "AddPerson";
}
AddPerson
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Gender)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.Gender, Model.Sex.ToSelectList())
@Html.ValidationMessageFor(model => model.Sex)
</div>
<div class="editor-field">
@{Html.RenderPartial("_Address", Model.HomeAddress);}
</div>
<div class="editor-field">
@{Html.RenderPartial("_Address", Model.DeliveryAddress);}
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@Html.ActionLink("Back to List", "Index")I am still new to MVC. I am currently having a add person page, the page includes person first name, last name and home address which is complex object (include addressline1,addressline2, state etc), delivery address (same to home address).
So I'd like to create a partial view which use to display address, but when I submit the form, I can't distinguish which value is for home address, coz home address and delivery address render the same name on the form.
MyClass
Public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int DateOfBirthMonth { get; set; }
public Gender Sex { get; set; }
public AddressModel HomeAddress { get; set; }
public AddressModel DeliveryAddress { get; set; }
public Person()
{
HomeAddress = new AddressModel();
DeliveryAddress = new AddressModel();
}
}
public class AddressModel
{
public string Addr1 { get; set; }
public string Addr2 { get; set; }
public string Suburb { get; set; }
}
public enum Gender
{
Male = 1,
Female = 2
}
My Address Partial View
@model MvcApplication1.Models.AddressModel
<fieldset>
<legend>Address</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Addr1)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Addr1)
@Html.ValidationMessageFor(model => model.Addr1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Addr2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Addr2)
@Html.ValidationMessageFor(model => model.Addr2)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Suburb)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Suburb)
@Html.ValidationMessageFor(model => model.Suburb)
</div>
Add Person page
@using MvcApplication1.Models
@model MvcApplication1.Models.Person
@{
ViewBag.Title = "AddPerson";
}
AddPerson
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Gender)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.Gender, Model.Sex.ToSelectList())
@Html.ValidationMessageFor(model => model.Sex)
</div>
<div class="editor-field">
@{Html.RenderPartial("_Address", Model.HomeAddress);}
</div>
<div class="editor-field">
@{Html.RenderPartial("_Address", Model.DeliveryAddress);}
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@Html.ActionLink("Back to List", "Index")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里发现了一个可能的问题:相同复杂类型的两个嵌套模型属性< /a>
在 AddPerson 视图中,您可以使用:
而不是:
I found a likely problem here: two nested model properties of same complex type
In your AddPerson View, you can use:
instead of: