I am not familiar with MVC yet. I currently have a page with an addition, the page includes the person’s name, surname and home address, which is a complex object (including address bar1, address bar2, status, etc.), delivery address (same as home address).
So, I would like to create a partial view that is used to display the address, but when I submit the form, I can’t distinguish which value for the home address, internal address and delivery address display the same name on the form.
My class
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 Partial View Address
@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 user 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 ("Return to List", "Index")
source share