在 ASP.NET MVC 3 中应用数据注释时,您打算如何使用提示、描述、排序
我有一个视图模型,其属性如下所示:
[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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并非所有内置的 EditorTemplates 都利用所有 DataAnnotations,但是当您编写自己的 EditorTemplates 时,您可以利用它们。
排序并不真正适用,除非您正在执行
DisplayForModel
或EditorForModel
,其中它为模型上的所有属性显示多个编辑器,然后它可以适当地对编辑器进行排序。如果您想利用
Description
和Prompt
元数据,您可以编写自己的String
EditorTemplate: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
orEditorForModel
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
andPrompt
metadata you could write your ownString
EditorTemplate: