Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple forms in ASP.NET MVC

Context
Let`s say i have:
In layout Site.Master:

<div class="leftColumn">
    <asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
<div class="rightColumn">
    <% Html.RenderPartial("_Login"); %>
    <asp:ContentPlaceHolder ID="SideContent" runat="server" />
</div>

Login partialView looks like:

<form action="/myApp/Account/Login" method="post">
    <input name="name" />Name<br />
    <input name="password" type="password" />Password<br />
    <button>Login</button>
</form>

Is it possible to update only login widget form, not the entire content page?

like image 443
Arnis Lapsa Avatar asked May 05 '09 00:05

Arnis Lapsa


People also ask

Can we use multiple Beginform in MVC?

Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.

Can we have two form tag in ASPX page?

The conclusion is that you are allowed to use multiple form tags on a page, as long as only one has the runat="server" attribute.

Can one view have multiple controllers?

Yes, It is possible to share a view across multiple controllers by putting a view into the shared folder. By doing like this, you can automatically make the view available across multiple controllers.


2 Answers

If you are referring to a http post, only a post initiated (it can also be initiated by javascript) by a submit button from within the form will be posted to the server.

If your forms are nested then this won't work. The outer form will always post to the server.

In the sample HTML below, clicking on the submit button on the first form will not send the values from the second form to the server. Likewise, clicking the second submit button won't post the values from the first form.

<html>
...
  <body> 
    <div>

      <form action="/Login/Login" method="post">
        <input type="text" name="username" value="" />
        <input type="text" name="passowrd" value="" />
        <input type="submit" name="login" value="Login" />
      </form>


      <form action="/Login/AdminLogin" method="post">
        <input type="text" name="username" value="" />
        <input type="text" name="passowrd" value="" />
        <input type="submit" name="login" value="Login Admin" />
      </form>
    </div>
</body>
</html>

If you only wish to update/change one of the form section, then no this can not be done without using javascript and performing a javascript post(aka Ajax).

like image 90
Chuck Conway Avatar answered Oct 12 '22 22:10

Chuck Conway


If you build a controller method that accepts a FormCollection and your view has two forms defined, the formcollection returned will either be populated with values from form A or form B. You can inspect the formCollection and branch your logic based on the value therein. If you want the be very explicit you could have the same hidden variable occur in both forms with a value that would help your make your choice.

That's one approach. there are a few ways to deal with this I'm sure.

like image 42
MarkDav.is Avatar answered Oct 13 '22 00:10

MarkDav.is