Tuesday 15 May 2012

c# - ASP.NET MVC 5, how to enable validation annotation on viewmodel that composes other view models? -


OK, I'm creating a very complicated user profile system in a social network application that I'm building. There are tabs in the profile page that separates the information of each profile of the user: a user profile viewmodel at the top of basic, education, jobs everything, which is the internal visual model of the original visual model, the education view model and the job view model. Creates. Consider the structure as below:

  public class profiles ViewModel {public string id {get; Set; } Public Basic ViewModel Basic {Received; Set; } Get public education videodel education {receive; Set; } See public jobs Model job {get; Set; }} Public square Basic ViewModel {public string first name {get; Set; } Public String LastName {get; Set; } Public Date Time? Date of registration {receive; Set; } Public Date Time? DateOfBirth {get; Set; } Public string gender {receipt; Set; } Receive the public string phone number { Set; } [Display (name = "biography")] {receives public string biography; Set; } Public string nickname {get; Set; } Public String Favorites Quotes {get; Set; }} Public class education visual model {public string education status {receipt; Set; } Public listing & lt; University & gt; Universities {Received; Set; } Receive public string coursestats { Set; } Public String Course Specialization {get; Set; } Public listing & lt; String & gt; Education Agent {Receive; Set; }} Public Class JobVivodel {public string workstats {get; Set; } Public listing & lt; Company & gt; Companies {Get; Set; }} Public Essentials Organization {public string name {get; Set; } Public Date Time? Year {receives; Set; } Get Public At Time Peel { Set; }} Public Square University: Organization {public string degree {get; Set; } Public string business {get; Set; }} Public Sector Company: Organization {public string website {get; Set; } Public string status {get; Set; }}  

So the question is, does the data annotation (both server and client side) work for a model model in which there is such a composite structure? If so, do I simply place the annotations as I normally do with a simple visual model? If not, how can I get it in alternative ways? Please help.

A single-view model can include other view models like this:

This model is Server Side:

  [Serialable] Public category MyBigViewModel: IValidatableObject {Public MyBigViewModel () {MyOtherViewModel = new MyOtherViewModel (); MyThirdViewModel = New MyThirdViewModel (); } Public MyOtherViewModel {get; Set;} Public MyThiddViewModel {get; Set;} Public Zero Post () {// You can post something like this, like Postback // maybe the method here processes data on MyOtherViewModel.Post (); }}  

The controller can look like this:

  Public function userUvist (MyViewView Model UVM) {if (ModelState.IsValid) {uvm.Post ( ); See Return (UVM); } See Return (UVM); }  

You can apply IValidateableObject to "server side" verification. However, in the above example, we would like that each view-model has to "add" itself for verification.

Each Voododal property can only use "implicit" data annotations in that vododal, this is a great way of "doing it" where you want.

I often use multiple viewloads in the main VM and pass them partially as needed.


No comments:

Post a Comment