上面写的意思是 MVC 引擎首先在 shared 中搜索视图,如果没有找到,它会在 ~/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
(如果您不提供模板名称hint,剃刀引擎默认为类型(int、boolean、string 甚至您定义的自定义类类型)
如果我认为便便是共享视图,那么便便相关的视图代码在哪里?
在上述多个地点。这允许您为每个控制器创建poo 特定视图和/或共享poo 视图。随心所欲。
当这一行将执行@Html.DisplayFor(m => m.Name) 那么会发生什么。
引擎将在上述文件夹中搜索模板。如果没有找到它,它会在相同的文件夹中查找object.cshtml/vbhtml。如果找到该文件,则执行该文件,否则执行默认的内部 object 代码显示。
MVC 将在哪里找到 yourTemplateName.cshtml 文件?
在上面的相同目录中。你必须明白它一遍又一遍地做同样的事情,它是asp.net-mvc的convention。
在 ASP.Net MVC 中 UIHint 属性有什么用
这允许您覆盖用于给定属性的模板。
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)