Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do i use two model in one view in MVC4?
I want to use two different models in my form.One is for registration another for login. I want to use both models in my main view. I added one model that contains two other model for registration and login. but am unable to fetch the values in controller for those models. values are coming as null.
See my code for model:
C#
public class RegistrationModel
   {
       [Required(ErrorMessage="First name is required")]
       public string FirstName { get; set; }

       [Required(ErrorMessage="Last name is required")]
       public string LastName {get; set;}

       [Required(ErrorMessage="Email is required")]
       [RegularExpression("^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$", ErrorMessage = "Invalid email")]
       public string Email { get; set; }

       [Required(ErrorMessage="Age is required")]
       [RegularExpression("[0-9]{1,2}", ErrorMessage = "Invalid age")]
       public int Age { get; set; }

       [Required(ErrorMessage="Date of birth is required")]
       public string Dob { get; set; }

       [Required(ErrorMessage="Password is required")]
       public string Pwd { get; set; }

       [Required(ErrorMessage = "Confirm Password is required")]
       [Compare("Pwd",ErrorMessage="Password mismatch")]
       public string Cpwd { get; set; }


   }
   public class LoginModel
   {
       [Required(ErrorMessage = "User name is required")]
       [Display(Name = "User name")]
       [RegularExpression("^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$", ErrorMessage = "Invalid email")]
       public string UserName { get; set; }

       [Required(ErrorMessage = "Password is required")]
       public string Pwd { get; set; }
   }
   public class Account
       {
           public LoginModel LoginModel{get; set;}
           public RegistrationModel RegisterModel {get; set;}
       }



Code in my view:
HTML
  @model Selfie.Models.Account
//Register
     @using (Html.BeginForm())     
        { 
            
           @Html.ValidationSummary(true)          
          <form  role="form">
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">First Name:</label>          
              <div class="col-sm-8">
                   @Html.TextBoxFor( model => model.RegisterModel.FirstName,null, new {@class="form-control",id="fn", placeholder=" First Name",required="true",maxlength="20"})
                   @Html.ValidationMessageFor(model => model.RegisterModel.FirstName) 
               <span id="erfn" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Last Name:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.LastName,null, new { @class = "form-control",id="ln", placeholder=" Last Name",required="true",maxlength="20"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.LastName)
              <span id="erln" style="color:red"></span> 
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Age:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.Age,null, new { @class = "form-control",id="ae", placeholder=" Age",required="true",maxlength="2"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.Age) 
              <span id="erae" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Email:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.RegisterModel.Email,null, new { @class = "form-control",id="em", placeholder=" Email",required="true",data_error="Bruh, that email address is invalid",type="email",maxlength="30"})
                    @Html.ValidationMessageFor(model => model.RegisterModel.Email) 
              <span id="erem" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.RegisterModel.Pwd,null, new { @class = "form-control",data_minlength="6",id="p",placeholder=" Password",required="true",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.RegisterModel.Pwd) 
              <span id="erpl" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Confirm Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.RegisterModel.Cpwd,null, new { @class = "form-control",id="cp",placeholder=" Confirm Password",required="true",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.RegisterModel.Cpwd) 
              <span id="erpm" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                  <label><input type="checkbox" id="chkter" required><a id="lt" href="#" data-toggle="modal" data-target="#myModalterms"> Agree terms and conditions</a> </label><br /><span id="erchk" style="color:red"></span>
              </div>
              </div>
         
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                   <button type="submit" class="btn btn-success" id="btnsub">Create Account</button>        
                   <button type="reset" class="btn btn-danger" id ="btnclr">Clear</button> 
              </div>
              </div>

             <div class="modal-footer">
             </div>
      </form>
        } 
//Login
@using (Html.BeginForm("Login", "Selfie", FormMethod.Post, new { }))
        {   
      
          @Html.ValidationSummary(true)       
         <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Email:</label>          
              <div class="col-sm-8">
                    @Html.TextBoxFor(model => model.LoginModel.UserName,null, new { @class = "form-control",id="lem", placeholder=" Email",maxlength="30"})
                    @Html.ValidationMessageFor(model => model.LoginModel.UserName) 
              <span id="lerem" style="color:red"></span>
              </div>
              </div>
              <div class="form-group" style="padding-removed15px; padding-removed15px">
              <label class="control-label col-sm-3">Password:</label>          
              <div class="col-sm-8">
                     @Html.TextBoxFor(model => model.LoginModel.Pwd,null, new { @class = "form-control",data_minlength="6",id="lp",placeholder=" Password",type="password",maxlength="20"})
                     @Html.ValidationMessageFor(model => model.LoginModel.Pwd) 
              <span id="lerpl" style="color:red"></span>
              </div>
              </div>
         <div class="form-group" style="padding-removed15px; padding-removed15px">
              <div class="control-label col-sm-3"></div>          
              <div class="col-sm-8">
                    <button type="submit" class="btn btn-success" id="btnlogin" name="command" value="user">Log In</button>
                    <button type="reset" class="btn btn-danger" id="btnlclr">Clear</button>
              </div>
              </div>
          
        <div class="divsplit"></div>
         <div class="modal-footer">
             <button type="submit" class="btn btn-danger" name="command" value="google">Sign In using Google</button>
             <button type="submit" class="btn btn-primary" name="command" value="facebook">Sign In using Facebook</button>
         
         </div>
        }      


Code for control:

C#
[HttpPost]
      public ActionResult Register(RegistrationModel registration)
      {
          // Here registration values are null even after am selecting values
      }

      [HttpPost]
      public ActionResult Login(LoginModel login)
      {

          // Here LoginModel values are null even after am selecting values
      }


CAn i use like
[HttpPost]
public ActionResult Login(AccountModel account)
{
string test = account.LoginModel.Username;
}will it work ?
Posted
Updated 12-Apr-15 20:30pm
v2
Comments
ramyajaya 12-Apr-15 19:16pm    
The model in the view is account , so the post method will receive only account model .

So your http post should be

[HttpPost]
public ActionResult Login(AccountModel account)
{
}
Am Gayathri 13-Apr-15 1:32am    
So can i get values if i use like below?
[HttpPost]
public ActionResult Login(AccountModel account)
{
string test = account.LoginModel.Username;
}

Thanks
ramyajaya 14-Apr-15 17:47pm    
Yes you can process it like that

1 solution

This article discusses many approaches to do this - Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^].
Multiple Models in Single View in MVC[^] will also help you.
 
Share this answer
 
v2
Comments
Am Gayathri 13-Apr-15 3:18am    
I want to fetch multiple model values on http post. Above links are explaing how to use multiple models in get method it seems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900