Why this ASP.NET Core DataAnnotation works in ViewModel but not in Model alone


In my one ASP.NET Core app, a View is using following ViewModel. The underlying model of the ViewModel has an attribute StartDate. The model has DataAnnotion on StartDate but I'm not using the same DataAnnotation on the StartDate in ViewModel since I thought if DataAnnotation is in your Model then it gets aggregated to your viewmodels. And hence, the following View should display the StartDate as Date only. But the View is displaying StartDate as date and time, e.g. 9/30/2015 12:00:00 AM. On the other hand if I use DataAnnotation [DataType(DataType.Date)] on StartDate attribute in the ViewModel as well, the View correctly displays the StartDate as date only, say, 9/30/2015.

<strong>Question</strong>: Why DataAnnotation on the Model is not getting aggregated to ViewModel?


... [DataType(DataType.Date)] public DateTime StartDate { get; set; } ...


... public DateTime StartDate { get; set; } ...


... @Html.DisplayFor(t => t.StartDate) ...


Some DataAnnotations are used by MVC, some are used by EntityFramework (others are used by other frameworks, so it's not just EF and MVC). Some are used by both. For instance, [Required] is used by both EF and MVC, but for different reasons. EF uses it to mean non-nullable. MVC uses it to mean "requires a value in the textbox". However, the DataType attribute and many other types are MVC only. I know it's confusing, and there seems to be no good reference for which annotations are for which technology.

And this is a good reason why you need separate data models from view models. You may need a particular annotation on your data model (like [Required]) but don't want it in your ViewModel (because you want to accept no value in a field, and supply the value in a code-behind, for instance...


  • Resuse ViewModel in Asp.net MVC
  • Unclear on how to implement dependency injection
  • Accessing the property of a model in ASP.NET MVC View
  • Accesing properties in a UserControl from the MainWindow (WPF/MVVM)
  • Vue props data not updating in child component
  • formediting modals shows at wrong position
  • Converting datatype Char to Nvarchar
  • CloseOptionsMenu doesn't work?
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Jquery Knockout: ko.computed() vs classic function?
  • Add dynamic data to line chart from mysql database with highcharts
  • Combining two different ActiveRecord collections into one
  • Extract zip entries to another Zip file
  • pyodbc doesn't report sql server error
  • Array with custom indexes in Ionic2
  • Stop Bash Script if Hive Fails
  • Textfile Structure (tables)
  • How to define and use opencv mat of user type
  • ActiveRecord query for a count of new users by day
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Illegal mix of collations for operation for date/time comparison
  • Azure Cloud Service Web Role web pages do not load
  • Does CUDA 5 support STL or THRUST inside the device code?
  • ActionScript 2 vs ActionScript 3 performance
  • Release, debug version and Authorization Google?
  • To display the title for the current loaction in map in iphone
  • Traverse Array and Display in markup
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?