Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to do a login ,register page in ASP.NET MVC within one view. But application need to have 2 separate model class for login and register actions please check below code.

View
HTML
@using PAMWebApp.Models;
@model Tuple<LoginViewModel,RegisterViewModel>

  @using (Html.BeginForm("Login", "Home", FormMethod.Post, new { id = "login-form", role = "form", style = "display: block;", }))
     {
     <div class="form-group">
     @Html.TextBoxFor(m => m.Item1.UserName, new { @class = "form-control", id = "username", name = "username", type = "text", placeholder = "Username", tabIndex = 1 })
     </div>

     <div class="form-group">
      @Html.TextBoxFor(m => m.Item1.Password, new { @class = "form-control", type = "password", id = "password", name = "pass", placeholder = "Password", tabindex = 2 })
    </div>

    <div class="form-group">
     <div class="row">
       <div class="col-sm-6 col-sm-offset-3">
          <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In">
      </div>
       </div>
       </div>
 }

Controller
C#
[HttpPost]
    [AllowAnonymous]
    public ActionResult Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            string name = model.UserName;
            string pwd = model.Password;
        }
     }


Model
C#
public class LoginViewModel
    {
        [Required]
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    }

    public class RegisterViewModel
    {
        [Required]
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }


What I have tried:

It doesn't pass the values from view to controller. So please any one correct me, .It's working fine, if it is use one model
Posted
Updated 21-May-18 16:40pm
Comments
F-ES Sitecore 21-May-18 4:57am    
Your controller action isn't returning a view at all. Regardless, google "multiple models mvc" and you'll find lots of suggestions about how to tackle this, it's a frequently asked question. You generally create a new view model class that has your other models as properties.

1 solution

You can wrap your LoginViewModel and RegisterViewModel models in single model. This means that you will create a new class that holds your existing models so you could reference the new class in your View instead. For example:

C#
public class MemberModel{
    public LoginViewModel LoginModel { get; set; }
    public RegisterViewModel RegisterModel { get; set; }
    //add more properties here if necessary
}


You can then reference the new model like this in your View:

C#
@model PAMWebApp.Models.MemberModel 
 
Share this answer
 

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