Should my form be inside or outside my editor template?

I use editor templates in ASP.net MVC. Should I put the form inside the editor template or outside 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" /> } 

Beyond

~ / Views / Products / Create.cshtml

 @using(Html.BeginForm()) { @Html.EditorForModel() <input type="submit" value="Save" /> } 

~ / Views / Products / EditorTemplates / CreateProduct.cshtml

 @Html.EditorFor(model => model.Name) 
+4
source share
2 answers

Although none of the methods are β€œwrong,” I would definitely say that the outside is better .

A form contributes to the flow of your application. When you try to follow a stream, it usually goes to Controller> View, so placing a form in a partial view will not do anything, but it will give you headaches.

In my application, all form always on the main viewing page, therefore, all editor templates and user interface elements are not aware of the "flow" of the application.

+6
source

What is best for you, There are no right or wrong answers to this question.
Without a frame, the HtmlHelper places the <form> , and the Telerik Grid control places the <form> , so I think both ways are good.

But I would prefer to place the <form> outside the template, so it can be more flexible if you put the <form> inside the template, you may have nested forms that are not supported in any of the browsers.

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

+1
source

Source: https://habr.com/ru/post/1390713/


All Articles