Search code examples
asp.net-mvcasp.net-mvc-3viewmodeldata-annotationsdisplayattribute

ASP.NET MVC DataType.Text


Having the following view model in my ASP.NET MVC 3 application, I have a problem described below:

public class MyViewModel : ViewModelBase
{
  // having this line kills the one below it, while commenting it out solves the problem. WHY?
  [DataType(DataType.Text)]
  [Display(Name = "Name", Description = "Description", Prompt = "Prompt"]
  public new string MyField { get; set; }
}

As soon as I comment out [DataType(DataType.Text)] I can see all other things appear, but if used together, DataType.Text is kind of overriding one below it (at least it seems like it).

Why is this happening and how to workaround?

Thanks in advance.


Solution

  • Oh, just figured it out. In case somebody needs it: I have editor template that is under Shared/EditorTemplates/String.cshtml that does all the magic with DisplayAttribute, while [DataType(DataType.Text)] is actually redirecting string to be text and I need to create yet another EditorTemplate parallel to String.cshtl called Text.cshtml.

    Live and Learn!