Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one view and that view contains create user and login forms. For both i have different models. When user clicks on Register button i will pass register model values and for login click i pass login model values.I have added some validation in model also (You can see it my code below).
Since i have two model i have added one more model(Account) which holds both this model.Am pssing this model(Account) from view to controller on click of register or login.
The reason why Model is invalid all the time is, when user clicks on login button am passing account model which has both Register and login model but with only login details .All the values for register model will be null since user clicked on login (Only login data will be there).So in controller Model.Isvalid is always false since no values are there for register model.

So is there any other way to validate a particular model ?
What is the solution in this case?


HTML
@model Selfie.Models.Account
//Register
     @using (Html.BeginForm())     
        { 

                   @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) 
              
                    @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)
              
                   <button type="submit" class="btn btn-success" id="btnsub">Create Account</button>        

             
        } 
//Login
@using (Html.BeginForm("Login", "Selfie", FormMethod.Post, new { }))
        {   
      
         
                    @Html.TextBoxFor(model => model.LoginModel.UserName,null, new { @class = "form-control",id="lem"})
                    @Html.ValidationMessageFor(model => model.LoginModel.UserName) 
             
                     @Html.TextBoxFor(model => model.LoginModel.Pwd,null, new { @class = "form-control"})
                     @Html.ValidationMessageFor(model => model.LoginModel.Pwd) 
             
                    <button type="submit" class="btn btn-success" id="btnlogin">Log In</button>
         
        }    


Controler
C#
[HttpPost]
       public ActionResult Register(Account registration)
       {
           if(Model.Isvalid)
             {
                 string a= Account.fname;

               }
       }

       [HttpPost]
       public ActionResult Login(Accountlogin)
       {
           if(Model.Isvalid)
             {
              string a= Account.username;

              }
       }


Model
C#
public class RegistrationModel
   {
       [Required(ErrorMessage="First name is required")]
       public string FirstName { get; set; }

       [Required(ErrorMessage="Password is required")]
       public string Pwd { get; set; }
   }
   public class LoginModel
   {
       [Required(ErrorMessage = "User name is required")]
       [Display(Name = "User name")]
       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;}
       }
Posted
Updated 14-Apr-15 1:24am
v2

1 solution

1. You could split this into two separate views each one using its respective model. So one view uses RegistrationModel only and the other view uses LoginModel model only

2. On the login model you can set dummy values for fields of RegistrationModel
@Html.HiddenFor(model => model.RegistrationModel.FirstName , new { id= "dummy_id", Value = "dummy_value_to_pass_validation"})
 
Share this answer
 
Comments
Am Gayathri 15-Apr-15 11:08am    
I think i can create partial view for login.

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