45

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

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
user1030181
  • 1,995
  • 5
  • 26
  • 56
  • 4
    What 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
  • 4
    UIHint 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 Answers1

94

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)
tugberk
  • 57,477
  • 67
  • 243
  • 335