Using HTML.EditFor for an viewmodel inside a parent viewmodel
I'm new to MVC3 and razor so this might be obvious but no amount of googlefu has found an answer for me.
I have a ViewModel called user, inside 开发者_Python百科of user i have a 2nd viewmodel called address.
Address is used all over the place so i planned on having the following\
ViewModels:
- User -> {Fname, lname, Address}
- Business -> {companyname, Address}
- House -> {Number, Address}
- Address -> {addyline1, addyline2, city, state, zip}
Ala... user.address.addyline1, business.address.addyline1
All of these will have the Address inside them.
@Html.EditorFor(model => model.address)
Works fine but doesn't use the template I have created in
views->shared->editortemplate->address.cshtml
@Html.EditorFor(model => model.address, "Address")
Does not work at all.
I agree with @jbtule. just remove the file extension from the partial view's name and you should be fine.
Another thing you can do to control how your Address
objects get rendered without having to specify the partial view template every time is create what's called an EditorTemplate
. Here's how you do it:
Create a folder called EditorTemplates
under the Views->Shared
folder in your ASP.NET MVC solution.
In that folder, create a strongly typed view whose name matches the name of the type you want the editor for (in your case it should be called Address.cshtml
), and you can also set the scaffolding template to Edit
to have a ready-to-go editor generated for you. and you're all set! all is left is to have add
here's an example on how to do this (note that it uses ASP.NET MVC2, and of course, the ASPX view engine, however the idea is the same).
Hope this helps ;)
Well you don't use put "cshtml" portion in typically when you put the template name probably "_AddressPartial"
would work for you, but i'd think you want to just omit that argument and put your custom razor template in "~/Views/Shared/EditorTemplates/address.cshtml"
The name of the folder to place your Address.cshtml
is
EditorTemplates
and not EditorTemplate
(one small s
can make a lot of difference).
精彩评论