Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is use of UIHint attribute in MVC [duplicate]

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

like image 788
user1030181 Avatar asked Nov 19 '11 18:11

user1030181


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)
like image 65
tugberk Avatar answered Nov 01 '22 08:11

tugberk