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

- 9,640
- 6
- 60
- 73

- 1,995
- 5
- 26
- 56
-
4What have you read about it so far that was confusing? What have you tried? This isn't a good place to ask for information that is already in the documentation. – Ian Mercer Nov 19 '11 at 22:19
-
4UIHint is a very important feature and sadly, the documentation of this is poor and there is no much clear articles or examples in the internet. – Andre Figueiredo Dec 26 '13 at 11:37
1 Answers
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)

- 57,477
- 67
- 243
- 335
-
1
-
9Could you please provide an example of how you would create the "Poo" display template? – ZiGiUs Sep 04 '14 at 12:53
-
3
-
1
-
1The `Name` property is a `string`. By default, `@Html.EditorFor(m => m.Name)` looks for a `string` editor. To plug to a custom partial view editor, `Views/Shared/EditorTemplates/Name.cshtml` for instance, there are 2 options: `@Html.EditorFor(m => m.Name, "Name")` or `[UIHint("Name")] public string Name { get; set; }`. – Romain Deneau Nov 08 '17 at 15:41
-
-
A template is written like a view. Its model is the type and value passed to it to be displayed or edited. – Suncat2000 Dec 13 '22 at 13:00