Mou*_*Mou 1 asp.net-mvc razor display-templates
从这个url阅读关于UIHint 的内容MVC中UIHint属性的用途是什么
如果使用UIHint属性注释属性并在视图中使用EditorFor或DisplayFor,则ASP.NET MVC框架将查找通过UIHintAttribute指定的指定模板.它寻找的目录是:
~/Views/Shared/EditorTemplates
~/Views/Controller_Name/EditorTemplates
Run Code Online (Sandbox Code Playgroud)
~/Views/Shared/DisplayTemplates
~/Views/Controller_Name/DisplayTemplates
Run Code Online (Sandbox Code Playgroud)
上面写的意思是MVC引擎首先在共享搜索视图中找不到然后它会在〜/ Views/Controller_Name/DisplayTemplates中搜索视图吗?
我只是得到了一个代码,但它没有完整,所以无法正确理解它
public class Person {
[UIHint("Poo")]
public string Name { get; set; }
}
@model MyApp.Models.Person
<h2>My Person</h2>
@Html.DisplayFor(m => m.Name)
Run Code Online (Sandbox Code Playgroud)
如果我认为Poo是一个共享视图那么poo相关的视图代码在哪里?
当这一行执行时@Html.DisplayFor(m => m.Name)
会发生什么.
看到这段代码
@Html.EditorFor(model => model.ProductViewModel, "yourTemplateName")
Run Code Online (Sandbox Code Playgroud)
哪里MVC会找到你的文件yourTemplateName.cshtml?
谢谢
上面写的意思是MVC引擎首先在共享搜索视图中找不到然后它会在〜/ 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"
Run Code Online (Sandbox Code Playgroud)
然后
"~/Views/{1}/DisplayTemplates/{0}.cshtml",
"~/Views/{1}/DisplayTemplates/{0}.vbhtml",
"~/Views/Shared/DisplayTemplates/{0}.cshtml",
"~/Views/Shared/DisplayTemplates/{0}.vbhtml"
Run Code Online (Sandbox Code Playgroud)
哪里
0 = Template/Type name
1 = ControllerName
2 = AreaName
Run Code Online (Sandbox Code Playgroud)
(如果您没有提供模板名称提示,则剃刀引擎默认为类型(int,boolean,string,甚至是您定义的自定义类类型)
如果我认为Poo是一个共享视图那么poo相关的视图代码在哪里?
在上面的一个或多个位置.这允许您为poo
每个控制器和/或共享poo
视图创建特定视图.但是你想要这样做.
当这一行执行@ Html.DisplayFor(m => m.Name)时会发生什么.
引擎将在上面的文件夹中搜索模板.如果找不到,则object.cshtml/vbhtml
在相同的文件夹中查找.如果找到该文件则执行它,否则执行object
代码的默认内部显示.
哪里MVC会找到你的文件yourTemplateName.cshtml?
在上面的相同目录中.你必须明白这一点,做一遍又一遍同样的事情,它是一个约定的asp.net-MVC.
ASP.Net MVC中UIHint属性的用途是什么
这允许您覆盖用于给定属性的模板.
public class Person
{
[UIHint("Age")]
public DateTime Birthday { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
将尝试在上述位置查找"age.cshtml".由于UIHintAttribute
未密封,您还可以派生自己的属性并创建一些非常漂亮的模板:
public UIDateTimeAttribute : UIHintAttribute
{
public UIDateTimeAttribute(bool canShowSeconds)
: base("UIDateTime", "MVC")
{
CanShowSeconds = canShowSeconds;
}
public bool CanShowSeconds { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
然后您的模型可能如下所示:
public class Person
{
[UIDateTime(false)]
public DateTime Birthday { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2987 次 |
最近记录: |