I am using MVC3 razor. I have a scenario where I have to use a partial view multiple times on the same parent view. The problem I am having is that when the Parent View gets rendered, it generates same names and ids of the input controls within those partial views. Since my partial views are binded to different models, when the view is posted back on "Save" it crashes. Any idea how can i make the control id/names unique, probably some how prefix them ?
Awaiting
Nabeel
Best Answer
Personally I prefer using editor templates, as they take care of this. For example you could have the following view model:
and the following controller:
and inside the
Index.cshtml
view:and the last part is the editor template (
~/Views/Home/EditorTemplates/ChildViewModel.cshtml
):Using the
EditorFor
you can include the template for different properties of your main view model and correct names/ids will be generated. In addition to this you will get your view model properly populated in the POST action.