How are you going to use Prompt, Description, Ordering when applying data annotations in ASP.NET MVC 3

I have a view model with a property on it that looks like this:

[Display(Name = "Some Property", Description = "This is description", Prompt = "This is prompt")] [Required(ErrorMessage = RequiredFieldMessage)] public string SomeProperty { get; set; } 

But this does not seem redundant in view. Do you need to do extra work?

  <div class="editor-label"> @Html.LabelFor(model => model.SomeProperty ) </div> <div class="editor-field"> @Html.TextAreaFor(model => model.SomeProperty , 5, 80, null) @Html.ValidationMessageFor(model => model.SomeProperty ) </div> 
+6
source share
1 answer

Not all built-in EditorTemplates use all DataAnnotations, but they exist when you write your own EditorTemplates that you can use.

The order doesn’t really apply unless you run DisplayForModel or EditorForModel , where it shows several editors for all the properties of the model, so it can arrange the editor accordingly.

If you want to use the Description and Prompt metadata, you can write your own String EditorTemplate:

 @model string @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @title = ViewData.ModelMetadata.Description, @placeholder = ViewData.ModelMetadata.Watermark}) 
+14
source

Source: https://habr.com/ru/post/900943/


All Articles