ASP.NET MVC 5, how to enable validation annotation

2019-09-08 08:13发布

问题:

Well I have a very complex User Profile system in a social network application I am building. The profile page has tabs that distinguishes each category of user profile information: Basic, Education, Job. There is a UserProfileViewModel sitting on top of everything, which composes of inner view models such as BasicViewModel, EducationViewModel and JobViewModel. Consider the structure as below:

    public class ProfileViewModel
{
    public string id { get; set; }
    public BasicViewModel basic { get; set; }
    public EducationViewModel education { get; set; }
    public JobViewModel job { get; set; }
}

public class BasicViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? DateOfRegistration { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public string Gender { get; set; }
    public string PhoneNumber { get; set; }
    [Display(Name = "Biography")]
    public string Biography { get; set; }
    public string NickName { get; set; }
    public string FavoriteQuotes { get; set; }
}

public class EducationViewModel{
    public string EducationStatus { get; set; }
    public List<University> Universities { get; set; }
    public string CourseStatus { get; set; }
    public string CourseSpecialization { get; set; }
    public List<string> EducationEvents { get; set; }
}

public class JobViewModel
{
    public string WorkStatus { get; set; }
    public List<Company> Companies { get; set; }
}

public abstract class Organization
{
    public string Name { get; set; }
    public DateTime? Year { get; set; }
    public int TimePeiod { get; set; }
}

public class University: Organization
{
    public string Degree { get; set; }
    public string Profession { get; set; }
}

public class Company: Organization
{
    public string Website { get; set; }
    public string Position { get; set; }
}

So the question is, does data annotation for model validation(both server and client side) work for a model that has composite structure like this? If so, do I just place annotation like I usually do with simple view models? If not, how can I achieve this in alternative ways? Please help.

回答1:

Any single view model may contain other viewmodels like this:

This model is server side:

[Serializable]
public class MyBigViewModel : IValidatableObject 
    {
       public MyBigViewModel(){
          MyOtherViewModel = new MyOtherViewModel();
          MyThirdViewModel = new MyThirdViewModel();
      }
      public MyOtherViewModel {get;set;}
      public MyThiddViewModel {get;set;} 

    public void Post(){
           //you can do something here based on post back 
           //like maybe this where the post method here processes new data
           MyOtherViewModel.Post();

    }

}

The controller could look like this:

  public ActionResult UserList (MyBigViewModel uvm){
      if(ModelState.IsValid){
         uvm.Post();
         return View(uvm);

      }
    return View(uvm);
  }

You can implement the IValidateableObject to do "server side" validation. In the example above however, we want each viewmodel to "contain" it's own model for validation.

Each viewmodel property can use data annotations "contained" in only that viewmodel. It's a very nice way to "Contain" what you want where you want.

I very often use multiple Viewmodels in main VM and pass them in with partial views as needed.