asp.net-mvc 是否可以在剃刀中将一种形式嵌套在另一种形式中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7250031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:26:23  来源:igfitidea点击:

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

asp.net-mvcrazor

提问by Richard77

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.

事情进展顺利。不幸的是,我有一个页面有几个“删除”按钮。我想为每个删除按钮生成一个表单,以便它可以将要删除的项目的 id 发送回控制器。

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

知道在此之上已经有另一种形式,我可以这样做吗?

Thanks for helping

谢谢你的帮助

回答by Erik Funkenbusch

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.

正如 Mrchief 所说,HTML 规范禁止嵌套表单。由于 MVC 仅生成标准 HTML,因此您必须在规范的框架内工作。

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.

这就是为什么你真的应该只在需要它们的地方使用表单,而不是无处不在。

回答by Mrchief

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

请注意,根据W3 规范不允许嵌套表单

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.

每个表单都必须包含在 FORM 元素中。一个文档中可以有多个表单,但不能嵌套 FORM 元素。

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

有一个关于嵌套形式的警告的interseting文章在这里

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

在这种情况下,最好form为每个按钮生成一个而不是单个表单。

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

ASP.Net Web 表单通过使用runat=server属性(以及确保每个页面只允许一个表单)来限制您在页面上拥有多个表单。MVCforms是纯 HTML,因此您可以拥有多个。