Question

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)
Was it helpful?

Solution

Although neither way is "wrong", I'd definitely say that outside is better.

A form contributes to the flow of your application. When you try to follow the flow, it usually goes Controller > View, so putting a form into 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.

OTHER TIPS

What suit you best, There is no right and wrong answers to this question.
Non of the framework's HtmlHelper put <form> tag while Telerik's Grid control does put <form>, so I guess both ways are good.

But I would rather put the <form> outside the template, so it can be more flexible, if you put the <form> inside the template you might have nested forms which is not supported in any of the browsers.

so I would make a partial view for a <form> and an editor template for simple HtmlHelpers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top