background preloader

ModelMetaData

Facebook Twitter

Model Metadata and Validation Localization using Conventions. By default, ASP.NET MVC leverages Data Annotations to provide validation.

Model Metadata and Validation Localization using Conventions

The approach is easy to get started with and allows the validation applied on the server to “float” to the client without any extra work. However, once you get localization involved, using Data Annotations can really clutter your models. For example, the following is a simple model class with two properties. public class Character { public string FirstName { get; set; } public string LastName { get; set; }} Nothing to write home about, but it is nice, clean, and simple. Public class Character { [Display(Name="First Name")] [Required] [StringLength(50)]] public string FirstName { get; set; } [Display(Name="Last Name")] [Required] [StringLength(50)]] public string LastName { get; set; }} That’s busier, but not horrible.

Wow! So what can I do to get rid of all that noise? I wrote a custom PROOF OF CONCEPT ModelMetadataProvider that supports this approach. What Conventions Does It Apply? Providing minimal metadata. Validation with the Data Annotation Validators. Take advantage of the Data Annotation Model Binder to perform validation within an ASP.NET MVC application.

Validation with the Data Annotation Validators

Learn how to use the different types of validator attributes and work with them in the Microsoft Entity Framework. In this tutorial, you learn how to use the Data Annotation validators to perform validation in an ASP.NET MVC application. The advantage of using the Data Annotation validators is that they enable you to perform validation simply by adding one or more attributes – such as the Required or StringLength attribute – to a class property. Before you can use the Data Annotation validators, you must download the Data Annotations Model Binder. You can download the Data Annotations Model Binder Sample from the CodePlex website by clicking here. It is important to understand that the Data Annotations Model Binder is not an official part of the Microsoft ASP.NET MVC framework. Using the Data Annotation Model Binder Using the Data Annotation Validator Attributes Summary. MVC 3 AdditionalMetadata Attribute with ViewBag to Render Dynamic UI. A few months ago I blogged about using Model metadata to render a dynamic UI in MVC 2.

MVC 3 AdditionalMetadata Attribute with ViewBag to Render Dynamic UI

The scenario in the post was that we might have a view model where the questions are conditionally displayed and therefore a dynamic UI is needed. To recap the previous post, the solution was to use a custom attribute called [QuestionId] in conjunction with an “ApplicableQuestions” collection to identify whether each question should be displayed.

This allowed me to have a view model that looked like this: 1: [UIHint("ScalarQuestion")] 2: [DisplayName("First Name")] 3: [QuestionId("NB0021")] 4: public string FirstName { get; set; } 6: [UIHint("ScalarQuestion")] 7: [DisplayName("Last Name")] 8: [QuestionId("NB0022")] 9: public string LastName { get; set; } 11: [UIHint("ScalarQuestion")] 12: [QuestionId("NB0023")] 13: public int Age { get; set; } 15: public IEnumerable<string> ApplicableQuestions { get; set; } At the same time, I was able to avoid repetitive IF statements for every single question in my view: Diving into ASP.NET MVC 3 Model Metadata Providers. Storing metadata about your model is a great feature that ASP.NET MVC 2 introduced.

Diving into ASP.NET MVC 3 Model Metadata Providers

These metadata are described by the DataAnnotation attributes which is built into .NET Framework. Metadata also enable a great way to specify Validation attributes, so the MVC Validation system can use those DataAnnotation Metadata attributes to provide Validation. In this article, we will discuss the DataAnnotation’s DisplayAttribute and how ModelMetadataProviders operate within ASP.NET MVC framework.

The difference between DisplayNameAttribute and MVC 3 new DisplayAttribute You may already know that ASP.NET MVC 2 introduced the DisplayNameAttribute which is part of the System.ComponentModel namespace. Create Responsive Design Mobile Sites & Apps using Sitefinity - Free Trial ASP.NET MVC 3 now supports DisplayAttribute in System.ComponentModel.DataAnnotation namespace. So what is the real difference between the “DisplayAttribute”, and the “DisplayNameAttribute”?

Handing Resources ModelMetatdataProviders.