@Html.Display(
>>, 而不是@Html.DisplayFor(>
>>,如果您的模型在汇编时间时不知道,或者如果你喜欢用斜体,而不是用强体字进行工作的话。 例如,这2项是等同的(因为你的模式是某些类别):
@Html.DisplayFor(m => m.MyProperty)
and
@Html.Display("MyProperty")
But the additional cool feature of the Display() method is that it can also do the lookup in the ViewData, and not just in your Model class. For example, here is a way to display the HTML for the property on a random object, given that we know it has a property named "Blah" (the type of the object doesn t really matter):
@{ ViewData["itsawonderfullife"] = SomeObject; }
<div>@Html.Display("itsawonderfullife.Blah")</div>
This way, we are telling HtmlHelper
to look into the ViewData
, instead of our Model
, and to display the property Blah
of a given SomeObject
.