Question

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

No correct solution

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top