MVC中UIHint属性有什么用

发布于 2024-12-17 06:39:00 字数 59 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2024-12-24 06:39:00

UIHintAttribute 指定动态数据用于显示数据字段。

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

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

对于 EditorFor

~/Views/Shared/EditorTemplates

~/Views/Controller_Name/EditorTemplates

对于DisplayFor :

~/Views/Shared/DisplayTemplates

~/Views/Controller_Name/DisplayTemplates

如果您位于某个区域,它也适用于您的区域,如上所示。

以下是使用示例:

public class Person { 

    [UIHint("Poo")]
    public string Name { get; set; }
}

如果您尝试使用 EditorForDisplayForpoo 的分部视图代码>如下所示:

@model MyApp.Models.Person

<h2>My Person</h2>

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文