Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are you meant 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 to render anything extra in the view. Do you need to do some additional 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>
like image 928
4imble Avatar asked Nov 07 '11 14:11

4imble


1 Answers

Not all of the built in EditorTemplates take advantage of all of the DataAnnotations, but they are there for when you write your own EditorTemplates you can leverage them.

Ordering doesn't really apply unless you are doing DisplayForModel or EditorForModel where its showing multiple editors for all the properties on the model, it can then order the Editor's appropriately.

If you wanted to take advantage of the Description and Prompt metadata you could write your own String EditorTemplate:

@model string
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { 
    @title = ViewData.ModelMetadata.Description, 
    @placeholder = ViewData.ModelMetadata.Watermark})
like image 118
Paul Tyng Avatar answered Oct 22 '22 01:10

Paul Tyng