Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to nest one form inside another in razor?

I put the begin/end form statement in the layout page so that I don't have to repeat it on several pages. Below's a simplified version of the code.

@using(Html.BeginForm())
{
    @RenderBody()

    <input type = "submit" name = "nextButton" value = "Next-->" />
}

Things are working well. Unfortunately, I have a page that has several "Delete" buttons. I want to generate a form for each delete button so that it can send the id of the item to delete back to the controller.

Can I do that knowing that there's already another form on top of that?

Thanks for helping

like image 660
Richard77 Avatar asked Aug 30 '11 21:08

Richard77


2 Answers

As Mrchief says, the the HTML specs forbid nested forms. Since MVC just generates standard HTML, you have to work withinn the framework of the spec.

Why not just create two master layouts, and use the form based one most of the time, but use one without a form when you need more control over the embedded forms.

This is the why you should really only use forms exactly where they are needed, not just everywhere.

like image 137
Erik Funkenbusch Avatar answered Oct 23 '22 00:10

Erik Funkenbusch


Do note that nesting of forms is not allowed as per the W3 specs

Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested.

There is an interseting article about caveats of nesting forms here.

In this case, it is better to generate a form for each button instead of having a single form.

ASP.Net web forms restricted you from having multiple forms on the page by using runat=server attribute (and the framework ensuring that only one per page was allowed). MVC forms are pure HTML so you can have multiple of them.

like image 31
Mrchief Avatar answered Oct 23 '22 01:10

Mrchief