.net MVC 中 UIHint 属性的用途是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8196372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:02:50  来源:igfitidea点击:

What is use of UIHint attribute in MVC

.netasp.net-mvcdata-annotationsasp.net-mvc-uihint

提问by user1030181

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

谁能解释一下 UIHint 属性在 MVC 中的用途。我们为什么需要这个。以及何时以及如何使用 . 谢谢

回答by tugberk

UIHintAttributeSpecifies the template or user control that Dynamic Data uses to display a data field.

UIHintAttribute指定动态数据用来显示数据字段的模板或用户控件。

This is the MSDN description of UIHintAttribute. It firstly introduced for Dynamic Data applications and ASP.NET MVC also adapted it.

这是 UIHintAttribute 的 MSDN 描述。它首先针对动态数据应用程序引入,ASP.NET MVC 也对其进行了改编。

If you annotate a property with UIHint attribute and use EditorForor DisplayForinside your views, ASP.NET MVC framework will look for the specified template which you specified through UIHintAttribute. The directories it looks for is:

如果您使用 UIHint 属性注释属性并在您的视图中使用EditorForDisplayFor,ASP.NET MVC 框架将查找您通过UIHintAttribute. 它寻找的目录是:

For EditorFor:

对于EditorFor

~/Views/Shared/EditorTemplates

~/Views/Controller_Name/EditorTemplates

~/Views/Shared/EditorTemplates

~/Views/Controller_Name/EditorTemplates

For DisplayFor:

对于DisplayFor

~/Views/Shared/DisplayTemplates

~/Views/Controller_Name/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 poounder the specified directories if you try to output the model property with EditorForor DisplayForlike below:

poo如果您尝试使用EditorForDisplayFor如下所示输出模型属性,MVC 框架将查找在指定目录下命名的部分视图:

@model MyApp.Models.Person

<h2>My Person</h2>

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