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
- 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)
- Why did he even suddenly stick to the type
Stringif the field displays the type Int?