我有一个 View 模型,上面有一个属性,如下所示:
[Display(Name = "Some Property", Description = "This is description", Prompt = "This is prompt")]
[Required(ErrorMessage = RequiredFieldMessage)]
public string SomeProperty { get; set; }
但这似乎并没有在 View 中呈现任何额外的内容。您需要做一些额外的工作吗?
<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})
关于asp.net-mvc-3 - 在 ASP.NET MVC 3 中应用数据注释时,你打算如何使用提示、描述、排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8037905/