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

大城市里の小女人 提交于 2019-12-10 13:23:08

问题


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>

回答1:


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})


来源:https://stackoverflow.com/questions/8037905/how-are-you-meant-to-use-prompt-description-ordering-when-applying-data-annota

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!