简体   繁体   English

你可以在ASP.NET MVC的页面上有两个表单吗?

[英]Can you have two forms on a page with ASP.NET MVC?

Using web forms I know that you can only have one ASP.NET form on a page. 使用Web表单我知道您在页面上只能有一个ASP.NET表单。 I've done some implementations where I've used Javascript to add other forms to a page to support things like logon controls (that post back to Logon.aspx instead of the current page). 我已经完成了一些实现,其中我使用Javascript将其他表单添加到页面以支持登录控件(返回到Logon.aspx而不是当前页面)。 I'm wondering if the single form per page is still present in ASP.NET MVC or if this restriction has been lifted. 我想知道每个页面的单个表单是否仍然存在于ASP.NET MVC中,或者是否已解除此限制。

You'll probably want to use regular HTML forms in your MVC code with the action attribute pointing to the appropriate controller action. 您可能希望在MVC代码中使用常规HTML表单,并将action属性指向适当的控制器操作。 So you can have as many forms on the page as you want. 因此,您可以在页面上拥有任意数量的表单。

With ASP.NET MVC, you can actually use any presentation layer that you want. 使用ASP.NET MVC,您实际上可以使用所需的任何表示层。 The default view engine, ASP.NET Web Forms, has the same restriction of one server-side form ( runat="server" ) but with or without MVC, you could always use client-only forms. 默认视图引擎ASP.NET Web Forms对一个服务器端表单( runat="server" )具有相同的限制,但无论有没有MVC,您都可以始终使用仅客户端表单。

With client-only forms (i,e., doesn't have runat='server' and therefore is parsed as part of an opaque LiteralControl parsed instead of an HtmlForm control), you cannot include any server-side controls that need to be in a server-side form. 使用仅客户端表单(例如,没有runat='server' ,因此被解析为解析的opaque LiteralControl一部分而不是HtmlForm控件),您不能包含任何需要的服务器端控件以服务器端的形式。

Any code in the Web Form's markup (usually in <%= code blocks %> ) that call the MVC framework's APIs (like <%= Html.Button() %> ) are not parsed as ASP.NET web controls, so there is no restriction on where in the page those are used. Web窗体的标记中的任何代码(通常在<%= code blocks %> )调用MVC框架的API(如<%= Html.Button() %> )都不会被解析为ASP.NET Web控件,因此存在没有限制使用页面的位置。

如你所见负责使用您可以有很多HTML表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM