在MVC中使用UIHint属性有什么用

use*_*181 43 .net asp.net-mvc data-annotations asp.net-mvc-uihint

任何人都可以解释一下MVC中UIHint属性的用途.我们为什么需要这个.以及何时以及如何使用.谢谢

tug*_*erk 90

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

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

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

用于EditorFor:

〜/查看/共享/ EditorTemplates

〜/查看/ CONTROLLER_NAME/EditorTemplates

用于DisplayFor:

〜/查看/共享/ DisplayTemplates

〜/查看/ CONTROLLER_NAME/DisplayTemplates

如果您在某个区域,它也适用于您所在的区域.

以下是使用示例:

public class Person { 

    [UIHint("Poo")]
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

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

@model MyApp.Models.Person

<h2>My Person</h2>

@Html.DisplayFor(m => m.Name)
Run Code Online (Sandbox Code Playgroud)

  • 能否请您提供一个如何创建"Poo"显示模板的示例? (8认同)
  • 是的,请演示如何创建便便:J (2认同)