什么是MVC中UIHint属性的使用。任何人都可以请我提供一个如何使用它及其作用的简单例子。
答案 0 :(得分:35)
使用显示或编辑器模板时,UIHint会告诉它使用哪个模板:
[UIHint("SomeTemplate")]
public class MyViewModel
{
public string SomeProperty { get; set; }
}
如果您在Views / Shared / DisplayTemplates或Views / {Controller} / DisplayTemplates中创建一个名为SomeTemplate.ascx的显示模板(因为您是MVC2),那么它将在您执行时使用该模板:
@Html.DisplayForModel() // if Model is MyViewModel
或
@Html.DisplayFor(m => m.ModelProperty) // if ModelProperty is of type MyViewModel
修改强>
如果要在属性级别指定此项:
public class MyViewModel
{
[UIHint("Birthday")]
public DateTime DateOfBirth { get; set; }
}
您可以在/ Views / Shared或/ Views / {Controller}中的DisplayTemplates或EditorTemplates文件夹中创建名为Birthday的显示/编辑器模板。然后当你这样做:
@Html.DisplayFor(m => m.DateOfBirth)
或
@Html.EditorFor(m => m.DateOfBirth)
它将使用UIHint中指定的模板
答案 1 :(得分:16)
UIHint只能用于不在类声明上的属性作为Dismissile状态。我正在使用MVC3,所以这可能已经从MVC2改变了。
“属性'UIHint'在此声明类型上无效。它仅对'property,indexer,field'声明有效”