简体   繁体   English

如何在ASP.NET MVC应用程序中使用Razor-engine中的Html.Displar渲染ModelMetadata对象?

[英]How to render ModelMetadata object using Html.Displar in Razor-engine in ASP.NET MVC app?

I am trying to utilize the DisplayTemplates feature in razor-engine to automate rendering my display views. 我正在尝试利用razor-engine中的DisplayTemplates功能来自动渲染我的显示视图。

I scan my Model to find the correct ModelMetadata for each property that I want to display. 我扫描我的Model ,为我想要显示的每个属性找到正确的ModelMetadata But I am not able to render the properties using Html.DisplayFor correctly. 但我无法正确使用Html.DisplayFor呈现属性。

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)

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. 上面的代码“在显示正确的值方面”但是它没有使用导致格式错误的正确的DisplayTemplate

For example I have an editor template called Currency.cshtml which simply display Model.ToString("C") . 例如,我有一个名为Currency.cshtml的编辑器模板, Model.ToString("C")显示Model.ToString("C") I also have another display template called Status.cshtml which accepts boolean value and return "Active" or "Inactive". 我还有另一个名为Status.cshtml显示模板,它接受布尔值并返回“活动”或“非活动”。 Both display-template are located in ~/Views/Shared/DisplayTemplates . 两个display-template都位于~/Views/Shared/DisplayTemplates

But for some reason Html.DisplayFor() isn't looking for the proper template . 但由于某些原因, Html.DisplayFor()没有寻找合适的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; }
}

Currently my view shows a disabled checkbox for the Active property and a non-formatted number for my Rate property. 目前,我的视图显示Active属性的禁用复选框和我的Rate属性的非格式化数字。

How can I correctly display ModelMetadata object where the correct display-template is used based on the property data-type? 如何根据属性数据类型正确显示使用正确显示模板的ModelMetadata对象?

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. ModelMetadata包含DataTypeName属性,它包含由所产生的价值DataTypeAttribute ,这将是“货币”为您Rate特性和“状态”为你Status属性。 You can use this value in the 2nd argument of DisplayFor() 您可以在DisplayFor()的第二个参数中使用此值

ModelMetadata columnMetadata = elementsMetadata.First(x => x.PropertyName == column.PropertyName);
string template = columnMetadata.DataTypeName;
Html.DisplayFor(x => columnMetadata.Model, template)

Note also that you can use the UIHintAttribute which also sets the value of DataTypeName , for example 另请注意,您可以使用UIHintAttribute ,例如,它还设置DataTypeName的值

[Display(Name = "Status")] // use this rather that [DisplayName]
[UIHint("Status")]
public bool Active { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM