ASP.NET MVC 5,如何在组成其他视图模型的视图模型上启用验证注释? [英] ASP.NET MVC 5, how to enable validation annotation on viewmodel that composes other view models?
问题描述
在我正在构建的社交网络应用程序中,我有一个非常复杂的User Profile系统.配置文件页面上有一些选项卡,用于区分用户配置文件信息的每个类别:基本",教育",工作".在所有内容的顶部都有一个UserProfileViewModel,它由内部视图模型(例如BasicViewModel,EducationViewModel和JobViewModel)组成.考虑以下结构:
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.
推荐答案
任何单个视图模型都可能包含其他这样的视图模型:
Any single view model may contain other viewmodels like this:
此模型是服务器端的
[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();
}
}
控制器看起来像这样:
public ActionResult UserList (MyBigViewModel uvm){
if(ModelState.IsValid){
uvm.Post();
return View(uvm);
}
return View(uvm);
}
您可以实现IValidateableObject进行服务器端"验证.但是,在上面的示例中,我们希望每个视图模型包含"它自己的模型以进行验证.
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.
每个viewmodel属性只能在该viewmodel中使用包含"的数据注释.这是在所需位置包含"所需内容的一种非常不错的方法.
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.
我经常在主VM中使用多个Viewmodel,并根据需要将它们与部分视图一起传递.
I very often use multiple Viewmodels in main VM and pass them in with partial views as needed.
这篇关于ASP.NET MVC 5,如何在组成其他视图模型的视图模型上启用验证注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!