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:
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:
@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:
[HttpPost]
public ActionResult Register(RegistrationModel registration)
{
}
[HttpPost]
public ActionResult Login(LoginModel login)
{
}
CAn i use like
[HttpPost]
public ActionResult Login(AccountModel account)
{
string test = account.LoginModel.Username;
}will it work ?