ModelState for a subset of properties of model in ASP.Net MVC

I have a web application in ASP.Net MVC 4.

I have a model called User with many properties like Name, Email, Password, BirthDate, Gender, etc.

public class UserModel
{
    public string UsrId { get; set; }

    [Required(ErrorMessage = "Please enter the user's name.")]
    public string UsrName { get; set; }

    [Required(ErrorMessage = "Please enter the email.")]
    public string UsrEmail { get; set; }

    [Required(ErrorMessage = "Please enter the password.")]
    public string UsrPwd { get; set; }

    [Required(ErrorMessage = "Please select the gender")]
    public ParamData Gender { get; set; }

    [Required(ErrorMessage = "Please select birthdate")]
    public string UsrBirthDate { get; set; }

//.... other properties
}

I used ModelState validation in a Login Validation. For Login I used the properties Email and Password. Both properties had Annotations of type "Required" , to validate not to enter empty strings. I used a ViewModel "LoginViewModel" viewmodel which one its properties was "User" type. I called ModelState.IsValid and it worked ok.

public class LoginViewModel
{
    public UserModel LoginDat { get; set; }
    // other props of ViewModel  ...
}

Now I am doing a Registration (1st step of registration) validation. For that I have a "Register1ViewModel" viewmodel, and again, one of its properties is of type User model. But for registration (first step) I only need the following 3 User's properties: Name, Gender, BirthDate. Those properties are too in User Model. I wrote Required annotations for these 3 properties.

public class RegiViewModel
{
    public UserModel RegiDat { get; set; }
    // other props of ViewModel  ...
}

But when I call ModelState.IsValid it results false because it asks for Email and Password properties.

[HttPost]
public ActionResult RegisterStep1Post(RegiViewModel VM, string Answer)
{   
    if (ModelState.IsValid)
    {
        RegiViewModel VM2 = GetSomeStuff(VM); 
        return View("Step2Validation", VM2);  // Ok go next step

    }
    else
    {
        return View("RegiStep1ViewPost", VM); // return to form
    }        
}

Is there any way of use only a defined subset of a model's properties when using ModelState.IsValid? I don't want to duplicate my User model.


You need to break down your models.

For login purposes, I assume you only need a an e-mail and password, so your model should look like this:

public class LoginViewModel
{
    [Required(ErrorMessage = "Please enter the email.")]
    public string UsrEmail { get; set; }

    [Required(ErrorMessage = "Please enter the password.")]
    public string UsrPwd { get; set; }
}

Your registration model should contain only the information it cares about:

public class RegiViewModel
{
    [Required(ErrorMessage = "Please enter the user's name.")]
    public string UsrName { get; set; }

    [Required(ErrorMessage = "Please select the gender")]
    public ParamData Gender { get; set; }

    [Required(ErrorMessage = "Please select birthdate")]
    public string UsrBirthDate { get; set; }
}

At some point in the registration process, you will ask the user to enter an e-mail and password. At that time, you use the LoginViewModel class again.


Can't you just make a base class with the 3 properties needed in your login? And then derive your UserModel class from this one?

Another way but not really recommended is to create your own 'RequiredAttribute':

    public class SpecialRequired : RequiredAttribute
{    
    string _context = "";
    public string Context
    {
        get
        {
            return _context;
        }
        set
        {
            _context = value;
        }
    }

    public override bool IsValid(object value)
    {
        if (_context == "Register")
        {
            return true;
        }
        return false; // With some more own logic
    }
}

You could use it like this: [SpecialRequired(Context="Register")]

链接地址: http://www.djcxy.com/p/56596.html

上一篇: 模型中的MVC验证

下一篇: ModelState用于ASP.Net MVC中模型的属性子集