开发者

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 fo开发者_开发知识库r 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜