Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net MVC 4 multiple post via different forms

Right now I understand

if (IsPost){   //do stuff } 

checks all post methods on that page. However, I have 2 different forms posting 2 different information. These are a login form and a register form.

Is there a way I can check IsPost based on which form? For example,

if(Login.IsPost){ //do stuff } 

but how would I define the Login variable? My form looks like:

<form id="Login" method = "POST"> 

I have tried:

var Login = Form.["Login"] 

it did not work.

I will appreciate any help.

Thanks.

like image 672
Davidred15 Avatar asked Apr 03 '13 13:04

Davidred15


People also ask

Can you mix webforms and MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.

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 use multiple forms in a Web page?

A server-side form tag is the tag which has a runat="server" attribute. If this attribute is missing, then it's a typical HTML form tag. 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.


1 Answers

In an MVC view, you can have as many forms with as many fields as you need. To keep it simple, use a single view model with all the properties you need on the page for every form. Keep in mind that you will only have access to the form field data from the form that you submit. So, if you have a login form and registration form on the same page you would do it like this:

LoginRegisterViewModel.cs

public class LoginRegisterViewModel {     public string LoginUsername { get; set; }     public string LoginPassword { get; set; }      public string RegisterUsername { get; set; }     public string RegisterPassword { get; set; }     public string RegisterFirstName { get; set; }     public string RegisterLastName { get; set; } } 

YourViewName.cshtml

@model LoginRegisterViewModel  @using (Html.BeginForm("Login", "Member", FormMethod.Post, new {})) {      @Html.LabelFor(m => m.LoginUsername)     @Html.TextBoxFor(m => m.LoginUsername)      @Html.LabelFor(m => m.LoginPassword)     @Html.TextBoxFor(m => m.LoginPassword)      <input type='Submit' value='Login' />  }  @using (Html.BeginForm("Register", "Member", FormMethod.Post, new {})) {      @Html.LabelFor(m => m.RegisterFirstName)     @Html.TextBoxFor(m => m.RegisterFirstName)      @Html.LabelFor(m => m.RegisterLastName)     @Html.TextBoxFor(m => m.RegisterLastName)      @Html.LabelFor(m => m.RegisterUsername)     @Html.TextBoxFor(m => m.RegisterUsername)      @Html.LabelFor(m => m.RegisterPassword)     @Html.TextBoxFor(m => m.RegisterPassword)      <input type='Submit' value='Register' /> 

}

MemberController.cs

[HttpGet] public ActionResult LoginRegister() {      LoginRegisterViewModel model = new LoginRegisterViewModel();      return view("LoginRegister", model); }  [HttpPost] public ActionResult Login(LoginRegisterViewModel model) {  //do your login code here }  [HttpPost] public ActionResult Register(LoginRegisterViewModel model) {  //do your registration code here } 

Do not forget, when calling BeginForm, you pass the Controller name without "Controller" attached:

@using (Html.BeginForm("Login", "Member", FormMethod.Post, new {})) 

instead of:

@using (Html.BeginForm("Login", "MemberController", FormMethod.Post, new {})) 
like image 117
jpshook Avatar answered Oct 08 '22 09:10

jpshook