Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIHint Attribute in MVC

Tags:

What is Use of UIHint Attribute in MVC . Can anyone please provide me a simple example of how to use it and what it does.

like image 529
user1030181 Avatar asked Nov 09 '11 18:11

user1030181


People also ask

What is UIHint MVC?

The UIHint property specifies which field template to use when a specific column is rendered. The UIHint property can point to one of the templates provided in Dynamic Data or to a custom template. For example, you can create a custom field template named RedText_Edit.


2 Answers

When using a Display or Editor template, UIHint will tell it which template to use:

[UIHint("SomeTemplate")] public class MyViewModel {      public string SomeProperty { get; set; } } 

If you create a Display template called SomeTemplate.ascx (since you are MVC2) in the Views/Shared/DisplayTemplates or Views/{Controller}/DisplayTemplates then it will use that template when you do:

@Html.DisplayForModel() // if Model is MyViewModel 

or

@Html.DisplayFor(m => m.ModelProperty) // if ModelProperty is of type MyViewModel 

edit
If you want to specify this on a property level:

public class MyViewModel {     [UIHint("Birthday")]     public DateTime DateOfBirth { get; set; } } 

You could create a display/editor template called Birthday in the DisplayTemplates or EditorTemplates folder in either /Views/Shared or /Views/{Controller}. Then when you do:

@Html.DisplayFor(m => m.DateOfBirth) 

or

@Html.EditorFor(m => m.DateOfBirth) 

It will use the template specified in UIHint

like image 115
Dismissile Avatar answered Oct 14 '22 19:10

Dismissile


UIHint can only be used on a property not on a class declaration as Dismissile states. I am using MVC3 so this may have changed from MVC2.

"Attribute 'UIHint' is not valid on this declaration type. It is only valid on 'property, indexer, field' declarations"

like image 23
Ant Avatar answered Oct 14 '22 19:10

Ant