Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

asp.net mvc - UIHint Attribute in MVC

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...