简体   繁体   中英

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. 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). I'm wondering if the single form per page is still present in ASP.NET MVC or if this restriction has been lifted.

You'll probably want to use regular HTML forms in your MVC code with the action attribute pointing to the appropriate controller 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. 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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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