What is use of UIHint attribute in MVC [duplicate]

…衆ロ難τιáo~ 提交于 2019-11-27 21:00:58

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

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

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:

For EditorFor:

~/Views/Shared/EditorTemplates

~/Views/Controller_Name/EditorTemplates

For DisplayFor:

~/Views/Shared/DisplayTemplates

~/Views/Controller_Name/DisplayTemplates

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

Here is the usage sample:

public class Person { 

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

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!