问题
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")回答1:
I found a likely problem here: two nested model properties of same complex type
In your AddPerson View, you can use:
@{Html.EditorFor(model => model.HomeAddress, "_Address");}
@{Html.EditorFor(model => model.DeliveryAddress, "_Address");}
instead of:
<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>
来源:https://stackoverflow.com/questions/6209520/same-partial-view-on-one-page-how-to-handle-binding