UIHint属性的用途是什么?

MVC中UIHint属性的用途。 我们为什么需要这个。 以及何时和如何使用?

UIHintAttribute指定动态数据用来显示数据字段的模板或用户控件。

这是UIHintAttribute的MSDN描述。它首先针对动态数据应用程序引入,并且ASP.NET MVC也对其进行了修改。

如果使用UIHint属性为属性添加注释并在视图中使用EditorForDisplayFor内部视图,则ASP.NET MVC框架将查找您通过指定的指定模板UIHintAttribute它查找的目录是:

对于EditorFor

~/Views/Shared/EditorTemplates

~/Views/Controller_Name/EditorTemplates

对于DisplayFor

~/Views/Shared/DisplayTemplates

~/Views/Controller_Name/DisplayTemplates

如果您在某个区域,则它也适用于您所在的区域,就像上面一样。

这是用法示例:

public class Person { 

    [UIHint("Poo")]
    public string Name { get; set; }
}

MVC框架会寻找名为局部视图poo指定的目录下,如果你尝试输出的模型属性与EditorForDisplayFor象下面这样:

@model MyApp.Models.Person

<h2>My Person</h2>

@Html.DisplayFor(m => m.Name)

如何创建“ Poo”显示模板?

poo.cshtml的内容:
@model string 
@{ <span class='poo'>@Model</span> }

Name属性是string

默认情况下,@Html.EditorFor(m => m.Name)查找string编辑器。

Views/Shared/EditorTemplates/Name.cshtml

例如,要插入自定义的局部视图编辑器

有2个选项:

@Html.EditorFor(m => m.Name, "Name")

[UIHint("Name")] 

public string Name { get; set; }

来自:https://stackoverflow.com/questions/8196372/what-is-use-of-uihint-attribute-in-mvc

原文地址:https://www.cnblogs.com/djd66/p/14821840.html