只是从这个 url What is use of UIHint attribute in MVC 阅读有关 UIHint 的信息
如果你用 UIHint 属性注释一个属性 并在您的 View 中使用 EditorFor 或 DisplayFor, ASP.NET MVC 框架将查找指定的 您通过 UIHintAttribute 指定的模板。 它寻找的目录是:
对于编辑者:
~/Views/Shared/EditorTemplates
~/Views/Controller_Name/EditorTemplates
对于显示对象:
~/Views/Shared/DisplayTemplates
~/Views/Controller_Name/DisplayTemplates
上面写的意思是 MVC 引擎首先在共享中搜索 View ,如果没有找到,那么它将在 ~/Views/Controller_Name/DisplayTemplates 中搜索 View ?
我刚得到一个代码,但它不完整,所以无法正确理解它
public class Person {
[UIHint("Poo")]
public string Name { get; set; }
}
@model MyApp.Models.Person
<h2>My Person</h2>
@Html.DisplayFor(m => m.Name)
如果我认为 Poo 是共享 View ,那么与 poo 相关的 View 代码在哪里?
当这一行将执行 @Html.DisplayFor(m => m.Name)
时会发生什么。
查看这段代码
@Html.EditorFor(model => model.ProductViewModel, "yourTemplateName")
MVC 会在哪里找到文件 yourTemplateName.cshtml?
谢谢
最佳答案
the above write up means MVC engine first search view in shared if not found then it will search view in ~/Views/Controller_Name/DisplayTemplates ?
倒过来,搜索模式是(准确):
(如果在一个区域)
"~/Areas/{2}/Views/{1}/DisplayTemplates/{0}.cshtml",
"~/Areas/{2}/Views/{1}/DisplayTemplates/{0}.vbhtml",
"~/Areas/{2}/Views/Shared/DisplayTemplates/{0}.cshtml",
"~/Areas/{2}/Views/Shared/DisplayTemplates/{0}.vbhtml"
然后
"~/Views/{1}/DisplayTemplates/{0}.cshtml",
"~/Views/{1}/DisplayTemplates/{0}.vbhtml",
"~/Views/Shared/DisplayTemplates/{0}.cshtml",
"~/Views/Shared/DisplayTemplates/{0}.vbhtml"
在哪里
0 = Template/Type name
1 = ControllerName
2 = AreaName
(如果您没有提供模板名称提示,razor 引擎默认为类型(int、boolean、string 甚至您定义的自定义类类型)
if i think Poo is a shared view then where is poo related view code?
在上面的另一个位置。这允许您为每个 Controller 创建 poo
特定 View 和/或共享 poo
View 。随心所欲。
when this line will execute @Html.DisplayFor(m => m.Name) then what will happen.
引擎将在上述文件夹中搜索模板。如果找不到,它会在相同的文件夹中查找 object.cshtml/vbhtml
。如果找到该文件,它会执行它,如果没有,它会执行默认的内部 object
代码显示。
where MVC will find the file yourTemplateName.cshtml?
在上面相同的目录中。你必须明白它一遍又一遍地做同样的事情,它是一个 convention的 asp.net-mvc .
What is use of UIHint attribute in ASP.Net MVC
这允许您覆盖用于给定属性的模板。
public class Person
{
[UIHint("Age")]
public DateTime Birthday { get; set; }
}
将尝试在上述位置寻找“age.cshtml”。由于 UIHintAttribute
不是密封的,您还可以派生自己的属性并创建一些非常漂亮的模板:
public UIDateTimeAttribute : UIHintAttribute
{
public UIDateTimeAttribute(bool canShowSeconds)
: base("UIDateTime", "MVC")
{
CanShowSeconds = canShowSeconds;
}
public bool CanShowSeconds { get; private set; }
}
那么你的模型可能看起来像:
public class Person
{
[UIDateTime(false)]
public DateTime Birthday { get; set; }
}
UIDateTime.cshtml
@model DateTime
@{
var format = "dd-MM-yy hh:mm:ss";
// Get the container model (Person for example)
var attribute = ViewData.ModelMetadata.ContainerType
// Get the property we are displaying for (Birthday)
.GetProperty(ViewData.ModelMetadata.PropertyName)
// Get all attributes of type UIDateTimeAttribute
.GetCustomAttributes(typeof(UIDateTimeAttribute))
// Cast the result as UIDateTimeAttribute
.Select(a => a as UIDateTimeAttribute)
// Get the first one or null
.FirstOrDefault(a => a != null);
if (attribute != null && !attribute.CanShowTime)
{
format = "dd-MM-yy hh:mm";
}
}
@Model.ToString(format)
关于asp.net-mvc - ASP.Net MVC 中 UIHint 属性的用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33066773/