UIHINT属性的用途是什么?

带参数的ASP.NET MVC编辑器模板UIHint      中UIHint属性的用途是什么

MVC中UIHint属性的用途。 我们为什么需要这个。 以及何时和如何使用?

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; }
}

MVC框架会寻找名为局部视图poo指定的目录下,如果你尝试输出的模型属性与EditorForDisplayFor象下面这样:

@model MyApp.Models.Person

<h2>My Person</h2>

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

 

如何创建“ Poo”显示模板?

poo.cshtml的内容:
@model string 
@{ <span class='poo'>@Model</span> }

 

Name属性是string

默认情况下,@Html.EditorFor(m => m.Name)查找string编辑器。

Views/Shared/EditorTemplates/Name.cshtml

例如,要插入自定义的局部视图编辑器,

有2个选项:

@Html.EditorFor(m => m.Name, "Name")

[UIHint("Name")] 

public string Name { get; set; }

 

来自:https://stackoverflow.com/questions/8196372/what-is-use-of-uihint-attribute-in-mvc

posted on   大西瓜3721  阅读(78)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2020-10-14 MVC防止xss攻击 ——Html.AntiForgeryToken的AJAX提交
2015-10-14 C# String.Format大全
2015-10-14 epub3 in action: epub3文件格式简介

导航

点击右上角即可分享
微信分享提示