如何在ASP.NET MVC应用程序的Razor引擎中使用Html.Displar渲染ModelMetadata对象? [英] How to render ModelMetadata object using Html.Displar in Razor-engine in ASP.NET MVC app?

查看:75
本文介绍了如何在ASP.NET MVC应用程序的Razor引擎中使用Html.Displar渲染ModelMetadata对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用剃刀引擎中的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆