I am using editor templates in ASP.net MVC. Should I be putting the form inside the editor template or outside of the template?
Example:
Inside
~/Views/Products/Create.cshtml
@Html.EditorForModel()
~/Views/Products/EditorTemplates/CreateProduct.cshtml
@using(Html.BeginForm())
{
@Html.EditorFor(model => model.Name)
<input type="submit" value="Save" />
}
Outside
~/Views/Products/Create.cshtml
@using(Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="Save" />
}
~/Views/Products/EditorTemplates/CreateProduct.cshtml
@Html.EditorFor(model => model.Name)
Although neither way is "wrong", I'd definitely say that outside is better.
A
formcontributes to the flow of your application. When you try to follow the flow, it usually goes Controller > View, so putting aforminto a partial view will do nothing but give you headaches.In my application, all
forms are always in the primary view page, so that all editor templates and UI elements are unaware of the "flow" of the application.