3

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

1 Answer 1

4

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 Partial you use the EditorFor and related methods:

@Html.EditorFor(m => m.HomeAddress)

You can then use the auto-generated templates or define your own using an approach similar to partial views.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.