Suppose you have the following models:
public class PickUpLocationViewModel { public DateTime PuDate { get; set } public IAddressViewModel Address { get; set; } } public class AirportAddressViewModel: IAddressViewModel { public string Terminal { get; set; } } public class SeaportAddressViewModel: IAddressViewModel { public int DockNumber { get; set; } }
and then the action of the controller:
public ActionResult Index() { var model = new PickUpLocationViewModel { Address = new AirportAddressViewModel { Terminal = "North" } }; return View(model); }
and corresponding presentation:
@model PickUpLocationViewModel @Html.DisplayFor(x => x.Address)
Now you can define the appropriate display / editor templates:
~/Views/Shared/EditorTemplates/AirportAddressViewModel.cshtml :
@model AirportAddressViewModel @Html.DisplayFor(x => x.Terminal)
~/Views/Shared/EditorTemplates/SeaportAddressViewModel.cshtml :
@model SeaportAddressViewModel @Html.DisplayFor(x => x.DockNumber)
Now, based on a specific type, ASP.NET MVC automatically uses the correct template.
And when it comes to snapping, you'll need a custom connectivity device. I have illustrated this here: https://stackoverflow.com/a/318969/
Darin Dimitrov
source share