什么是利用MVC UIHint属性

问题描述:

任何人能请解释我有什么用的MVC UIHint属性。为什么我们需要这个。和何时以及如何使用。谢谢

Can anyone please explain me what is the use of UIHint attribute in MVC . Why do we need this. and when and how to use . Thanks

UIHintAttribute指定动态数据用于显示一个数据字段中的模板或用户控制。

UIHintAttribute Specifies the template or user control that Dynamic Data uses to display a data field.

这是UIHintAttribute的MSDN描述。它首先介绍了对动态数据的应用程序和ASP.NET MVC也适应了。

This is the MSDN description of UIHintAttribute. It firstly introduced for Dynamic Data applications and ASP.NET MVC also adapted it.

如果您有注释属性UIHint一个属性,使用 EditorFor DisplayFor 在你的看法,ASP.NET MVC框架会寻找你通过 UIHintAttribute 指定指定的模板。它查找的目录是:

If you annotate a property with UIHint attribute and use EditorFor or DisplayFor inside your views, ASP.NET MVC framework will look for the specified template which you specified through UIHintAttribute. The directories it looks for is:

有关 EditorFor

〜/查看/共享/ EditorTemplates

~/Views/Shared/EditorTemplates

〜/查看/ CONTROLLER_NAME / EditorTemplates

~/Views/Controller_Name/EditorTemplates

有关 DisplayFor

〜/查看/共享/ DisplayTemplates

~/Views/Shared/DisplayTemplates

〜/查看/ CONTROLLER_NAME / DisplayTemplates

~/Views/Controller_Name/DisplayTemplates

如果你是一个区域,它适用于您所在地区像上面为好。

If you are on an area, it applies to your area like above as well.

下面是使用示例:

public class Person { 

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

MVC框架会寻找名为便便指定目录下的局部视图如果您尝试输出 EditorFor $ C $的模型属性C>或 DisplayFor 象下面这样:

MVC framework will look for partial view named poo under the specified directories if you try to output the model property with EditorFor or DisplayFor like below:

@model MyApp.Models.Person

<h2>My Person</h2>

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