如何在ASP.NET MVC应用程序的Razor引擎中使用Html.Displar渲染ModelMetadata对象? [英] How to render ModelMetadata object using Html.Displar in Razor-engine in ASP.NET MVC app?
问题描述
我正在尝试使用剃刀引擎中的DisplayTemplates
功能来自动呈现显示视图.
I am trying to utilize the DisplayTemplates
feature in razor-engine to automate rendering my display views.
我扫描我的Model
以找到要显示的每个属性的正确ModelMetadata
.但是我无法使用Html.DisplayFor
正确渲染属性.
I scan my Model
to find the correct ModelMetadata
for each property that I want to display. But I am not able to render the properties using Html.DisplayFor
correctly.
我试图这样显示我的财产
I tried to display my property like so
// First I find the ModelMetadata for the property
// I call this statement in a loop and each iteration I pass a different PropertyName to get the correct model
ModelMetadata columnMetadata = elementsMetadata.First(x => x.PropertyName == column.PropertyName);
// I attempted to render the columnMetadata object like so
Html.DisplayFor(x => columnMetadata.Model)
上面的代码显示正确的值"有效,但是它没有使用正确的DisplayTemplate
,这会导致格式错误.
The above code works "in term of displaying the correct value" However it does not use the correct DisplayTemplate
which is causing the incorrect format.
例如,我有一个名为Currency.cshtml
的编辑器模板,该模板仅显示Model.ToString("C")
.我还有另一个名为Status.cshtml
的显示模板,该模板接受布尔值并返回有效"或无效".两个显示模板都位于~/Views/Shared/DisplayTemplates
.
For example I have an editor template called Currency.cshtml
which simply display Model.ToString("C")
. I also have another display template called Status.cshtml
which accepts boolean value and return "Active" or "Inactive". Both display-template are located in ~/Views/Shared/DisplayTemplates
.
但是由于某些原因,Html.DisplayFor()
找不到合适的template
.这是我的一个视图模型的一个示例
But for some reason Html.DisplayFor()
isn't looking for the proper template
. Here is an example of one of my view-models
public class ViewModel
{
public string Name { get; set; }
[DataType(DataType.Currency)]
public decimal Rate { get; set; }
[DataType("Status"), DisplayName("Status")]
public bool Active { get; set; }
}
当前,我的视图显示Active
属性的禁用复选框和Rate
属性的未格式化数字.
Currently my view shows a disabled checkbox for the Active
property and a non-formatted number for my Rate
property.
如何根据属性数据类型正确显示使用正确显示模板的ModelMetadata
对象?
How can I correctly display ModelMetadata
object where the correct display-template is used based on the property data-type?
推荐答案
ModelMetadata
包含DataTypeName
属性,该属性包含DataTypeAttribute
生成的值,该值将是Rate
属性的货币"和状态"为您Status
属性.您可以在DisplayFor()
ModelMetadata
contains a DataTypeName
property, which contains the value generated by the DataTypeAttribute
, which will be "Currency" for your Rate
property and "Status" for you Status
property. You can use this value in the 2nd argument of DisplayFor()
ModelMetadata columnMetadata = elementsMetadata.First(x => x.PropertyName == column.PropertyName);
string template = columnMetadata.DataTypeName;
Html.DisplayFor(x => columnMetadata.Model, template)
还请注意,您可以使用UIHintAttribute
,例如,它还会设置DataTypeName
的值
Note also that you can use the UIHintAttribute
which also sets the value of DataTypeName
, for example
[Display(Name = "Status")] // use this rather that [DisplayName]
[UIHint("Status")]
public bool Active { get; set; }
这篇关于如何在ASP.NET MVC应用程序的Razor引擎中使用Html.Displar渲染ModelMetadata对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!