There is a model

public class viewmodel { [Required(ErrorMessage = "Обязательное поле")] [StringLength(12, MinimumLength = 5, ErrorMessage = "Слишком длинное или короткое поле")] [Display(Name = "Имя чувака")] public string name { get; set; } [EmailAddress(ErrorMessage = "Введи адрес почты")] [Display(Name = "Почта")] public string mail { get; set; } [DataType(DataType.Date)] public DateTime? born { get; set; } [Range(2, 5)] public int? AnyNum { get; set; } } 

I have a twist

 <body> <div> @using(Html.BeginForm()) { @Html.LabelFor(x=>x.name)<br /> @Html.EditorFor(x=>x.name)<br /> @Html.ValidationMessageFor(x=>x.name)<br /> <br /> @Html.LabelFor(x => x.mail)<br /> @Html.EditorFor(x => x.mail)<br /> @Html.ValidationMessageFor(x => x.mail)<br /> <br /> @Html.LabelFor(x => x.AnyNum)<br /> @Html.EditorFor(x => x.AnyNum)<br /> @Html.ValidationMessageFor(x => x.AnyNum)<br /> <br /> <input type="submit" name="Send" /> } <br /> @Html.DisplayFor(x => x.name)<br /> @Html.DisplayFor(x => x.mail)<br /> @Html.DisplayFor(x => x.AnyNum)<br /> </div> </body> 

And there is a folder that overrides the DisplayTemplates template, in this folder is a template for the string type. It has the name String and model string . Here is the template code

 @model string <span style="color:blueviolet">@Model</span> 

In this case, the template simply changes the color of the text. But for some reason, an event with such content is thrown away. Элемент модели, переданный в словарь, имеет тип "System.Int32", но для этого словаря требуется элемент модели типа "System.String". when it comes to @Html.DisplayFor(x => x.AnyNum) - that is, to the data type Int. I previously thought that if the type is different from that specified in the template, then it is simply not processed by this template. But then he issued an exept. Therefore, there are a couple of questions

  1. How to make him see that this is not the type of data and just output by default? (to specify the names of the template in each element seems to me wrong, because it assumes a lot of output fields)
  2. Why did he even suddenly stick to the type String if the field displays the type Int?

    1 answer 1

    The fact is that the templates are applied to the properties of the model in a certain order:

    1. The template is explicitly specified in the Editor or Display method.
    2. The pattern is specified in the UIHint attribute UIHint
    3. The template is bound to the type specified via the DataType attribute DataType
    4. The pattern matches the class name of the property.
    5. If the property type is primitive, the String pattern is used.
    6. The template matches the base type of the property type.
    7. If the property type implements IEnumerable , then the Collection template is used.
    8. Otherwise, the Object pattern is used.

    In your case, rule 5 is triggered - for properties of a primitive type, unless otherwise specified, the String template is always used.

    The fix is ​​quite simple - delete the model from the template:

     <span style="color:blueviolet">@Model</span> 

    Or use any of the methods 1-4, which have a higher priority.