I am still trying to figure out how to create reusable partial views in MVC
Lets say I would like to create a partial view to display a form for submitting an address.
Then in my ViewModel I have two addresses (Home address & Work Address)
So I would think that in my view I call HTML.Partial for each one like this
@Html.Partial("Address", Model.HomeAddress)
@Html.Partial("Address", Model.WorkAddress)
but what happens is instead of the fields having names like HomeAddress.Street, HomeAddress.City etc. they just have the regular field names Street, City, etc. so the binder on the HTTPPost action has no idea what to do with them
Thanks in advance
Partial views where not designed to handle that scenario. What you are looking for are sub-editors. Take a look at Brad Wilson’s excellent series on editor templates: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
Instead of
Partialyou use theEditorForand related methods:You can then use the auto-generated templates or define your own using an approach similar to partial views.