加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 在ASP.NET MVC 3中应用数据注释时,如何使用提

发布时间:2020-12-15 23:06:43 所属栏目:asp.Net 来源:网络整理
导读:我有一个带有属性的视图模型,如下所示: [Display(Name = "Some Property",Description = "This is description",Prompt = "This is prompt")] [Required(ErrorMessage = RequiredFieldMessage)] public string SomeProperty { get; set; } 但这似乎没有在视
我有一个带有属性的视图模型,如下所示:
[Display(Name = "Some Property",Description = "This is description",Prompt = "This is prompt")]
    [Required(ErrorMessage = RequiredFieldMessage)]
    public string SomeProperty { get; set; }

但这似乎没有在视图中呈现任何额外的内容.你需要做一些额外的工作吗?

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

解决方法

并非所有内置的EditorTemplates都利用了所有的DataAnnotations,但是当你编写自己的EditorTemplates时,它们就可以利用它们.

除非您正在使用DisplayForModel或EditorForModel来显示模型上所有属性的多个编辑器,否则排序并不真正适用,它可以适当地对编辑器进行排序.

如果您想利用Description和Prompt元数据,您可以编写自己的String EditorTemplate:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读