Take the 2-minute tour ×

I did MVC 2 two years ago. I am now using MVC 4 and would like to confirm a few things.

1) In MVC 4, we do not need to create a ModelMetadata class to annotate with data annotations for data validation. We may simply annotate the model classes themselves.

In other words, if we are using the EDM generator to generate a model and corresponding classes for us, then we may create a new set of partial classes with the same names and use data annotation attributes on those partial classes themselves.

We do not need to create a new type of model metadata class and decorate that class with data annotation/attributes, like here: http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/validation-with-the-data-annotation-validators-cs

2) For data annotations to work, we need a reference only to System.ComponentModel.DataAnnotations and not to Microsoft.Web.Mvc.DataAnnotations.

3) We do not need to instantiate the default model binder in the Application_Start event in the Global.asax file.

Could you please confirm if my understanding is correct?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted
  1. As I remember, there's no difference between MVC 2 and MVC 4 when it comes to this. You didn't have to use MetadataType in MVC 2, you could have used partial classes for generated models.
  2. I'm not able to find any MSDN doc on Microsoft.Web.Mvc.DataAnnotations, but for data annotations to work with MVC you'd need System.ComponentModel.DataAnnotations and System.Web.Mvc (it contains some additional attributes like HiddenInputAttribute).
  3. No, you don't, it's instantiated by framework.
share|improve this answer
Thanks very much indeed. –  Water Cooler v2 Dec 13 '12 at 9:46
add comment

Actually, for question 1, you need to use Metadata classes to annotate the properties of of model-first EF classes in the same way as previously with MVC2. When you use code-first EF classes, you can annotate them directly - the annotations will be used for generating the database as well as for scaffolding the Views.

share|improve this answer
add comment

Your Answer


