1

このURLからUIHintについて読んでいるだけです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 エンジンが最初に共有のビューを検索し、見つからない場合は ~/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)

Poo が共有ビューだと思う場合、poo 関連のビュー コードはどこにありますか?

この行が実行さ@Html.DisplayFor(m => m.Name)れると何が起こるか。

このコードを参照してください

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

MVC は yourTemplateName.cshtml ファイルをどこで見つけますか?

ありがとう

4

1 に答える 1

6

上記の記述は、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 での 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 に答える