Following is my model class for User,
public class User
{
[System.ComponentModel.DataAnnotations.Key]
public int UserId { get; set; }
public Int16 RoleID { get; set; }
[DisplayName("First Name :")]
public string FirstName { get; set; }
[DisplayName("Last Name :")]
public string LastName { get; set; }
[DisplayName("Email :")]
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is required")]
[DisplayName("Password :")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Compare("Password")]
[Required(ErrorMessage = "Confirm password is required")]
[DisplayName("Confirm Password :")]
public string CPassword { get; set; }
}
Following code is in AccountController
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(User model, string returnUrl)
{
if(ModelState.IsValid) //This turns out to be always false
}
This goes to Login.cshtml
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset class="cf addForm">
@Html.LabelFor(model => model.Email)
@Html.TextBoxFor(model => model.Email, new { @class = "wd293 inputtext" })
@Html.ValidationMessageFor(model => model.Email)
@Html.LabelFor(model => model.Password)
@Html.PasswordFor(model => model.Password, new { @class = "wd293 inputtext" })
@Html.ValidationMessageFor(model => model.Password)
<div class="cf signBtn">
<input type="submit" value="Sign in" class="pageBtn alignleft savebtn" />
</div>
</fieldset>
}
Above model class should work fine for Register form i.e. where all details are required as mentioned e.g. Email/Password/ConfirmPassword
But for Login Form where only Email and Password would be required field so in that case ModelState.IsValid is always giving false.
I am in a dilemma what should be the solution, create another model class ? like for Login form there would be another model UserLoginViewModel which would have only 2 property email/password and for Register form UserRegisterViewModel which would have all required porperty ?
Please bear with me, if this sounds silly as I am fairly new to MVC4. Please let me know if further code is needed as well.
EDIT
public class MyDBContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Category> Categories { get; set; } // It has CategoryID,CategoryName properties
}
Above is my db context class, for saving Category in database I write following code,
new MyDBContext().Categories.Add(category);
So let say I created new class as UserRegisterViewModel
with all the attribute required for register form, in that case I need to convert this UserRegisterViewModel to User again, will this be okay or will be adding memory overhead ?
Conversion would be like
User newUser = new User(); //Assign all the properties from UserRegisterViewModel to this and save
new MyDBContext().Users.Add(newUser);
Please help.