1

只是从这个 url 阅读 UIHintUIHint 在 MVC 中 UIHint 属性的用途是什么

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

对于编辑对于:

~/Views/Shared/EditorTemplates

~/Views/Controller_Name/EditorTemplates

对于 DisplayFor:

~/Views/Shared/DisplayTemplates

~/Views/Controller_Name/DisplayTemplates

上面写的意思是 MVC 引擎首先在 shared 中搜索视图,如果没有找到,那么它将在 ~/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)

如果我认为便便是共享视图,那么便便相关的视图代码在哪里?

当这条线将执行@Html.DisplayFor(m => m.Name)然后会发生什么。

看到这个代码

@Html.EditorFor(model => model.ProductViewModel, "yourTemplateName")

MVC 会在哪里找到 yourTemplateName.cshtml 文件?

谢谢

4

1 回答 1

6

上面写的意思是 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

(如果您不提供模板名称提示,剃刀引擎默认为类型(int、boolean、string 甚至您定义的自定义类类型)

如果我认为便便是共享视图,那么便便相关的视图代码在哪里?

在上面的另一个位置。这允许您为poo每个控制器和/或共享视图创建特定poo视图。但是,您想这样做。

当这一行将执行 @Html.DisplayFor(m => m.Name) 那么会发生什么。

引擎将在上述文件夹中搜索模板。如果没有找到它,它会object.cshtml/vbhtml在相同的文件夹中查找。如果找到该文件,它就会执行它,否则它会执行object代码的默认内部显示。

MVC 会在哪里找到 yourTemplateName.cshtml 文件?

在上面的相同目录中。您必须了解这一点,它一遍又一遍地做同样的事情,这是asp.net 约定

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)
于 2015-10-11T16:07:52.950 回答