asp.net-mvc MVC 中的 UIHint 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8069994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIHint Attribute in MVC
提问by user1030181
What is Use of UIHint Attribute in MVC . Can anyone please provide me a simple example of how to use it and what it does.
什么是 MVC 中 UIHint 属性的使用。任何人都可以给我一个简单的例子,说明如何使用它以及它的作用。
回答by Dismissile
When using a Display or Editor template, UIHint will tell it which template to use:
当使用 Display 或 Editor 模板时,UIHint 会告诉它使用哪个模板:
[UIHint("SomeTemplate")]
public class MyViewModel
{
public string SomeProperty { get; set; }
}
If you create a Display template called SomeTemplate.ascx (since you are MVC2) in the Views/Shared/DisplayTemplates or Views/{Controller}/DisplayTemplates then it will use that template when you do:
如果您在 Views/Shared/DisplayTemplates 或 Views/{Controller}/DisplayTemplates 中创建一个名为 SomeTemplate.ascx(因为您是 MVC2)的显示模板,那么它会在您执行以下操作时使用该模板:
@Html.DisplayForModel() // if Model is MyViewModel
or
或者
@Html.DisplayFor(m => m.ModelProperty) // if ModelProperty is of type MyViewModel
edit
If you want to specify this on a property level:
编辑
如果要在属性级别指定此内容:
public class MyViewModel
{
[UIHint("Birthday")]
public DateTime DateOfBirth { get; set; }
}
You could create a display/editor template called Birthday in the DisplayTemplates or EditorTemplates folder in either /Views/Shared or /Views/{Controller}. Then when you do:
您可以在 /Views/Shared 或 /Views/{Controller} 的 DisplayTemplates 或 EditorTemplates 文件夹中创建一个名为 Birthday 的显示/编辑器模板。然后当你这样做时:
@Html.DisplayFor(m => m.DateOfBirth)
or
或者
@Html.EditorFor(m => m.DateOfBirth)
It will use the template specified in UIHint
它将使用 UIHint 中指定的模板
回答by Ant
UIHint can only be used on a property not on a class declaration as Dismissile states. I am using MVC3 so this may have changed from MVC2.
UIHint 只能用在属性上,而不是像 Dismissile 状态那样用在类声明上。我正在使用 MVC3,所以这可能与 MVC2 有所不同。
"Attribute 'UIHint' is not valid on this declaration type. It is only valid on 'property, indexer, field' declarations"
“属性‘UIHint’在此声明类型上无效。它仅对‘属性、索引器、字段’声明有效”

