模型:
public class Address { public string Street { get; set; } public string City { get; set; } } public class MyViewModel { public Address Address { get; set; } }
控制器:
public class HomeController: Controller { public ActionResult Index() { var model = new MyViewModel(); return View(model); } [HttpPost] public ActionResult Index(MyViewModel model) { // the default model binder will take care of populating // your view model along with its complex properties ... } }
视图:
@model MyViewModel @using (Html.BeginForm()) { <div> @Html.LabelFor(x => x.Address.Street) @Html.EditorFor(x => x.Address.Street) </div> <div> @Html.LabelFor(x => x.Address.City) @Html.EditorFor(x => x.Address.City) </div> <p><button type="submit">OK</button></p> }