上記の記述は、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"
それから
"~/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 関連のビュー コードはどこにありますか?
上記の場所のもう 1 つ。これにより、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; }
}
上記の場所で「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)