获取脚手架以特定顺序生成字段 [英] Get scaffolder to generate fields in specific order

查看:96
本文介绍了获取脚手架以特定顺序生成字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使asp.net核心MVC用与默认默认字母顺序不同的顺序排列字段的Razor视图.我有一个简单的模型:

I am trying to get asp.net core MVC to scaffold a Razor View with fields in a different order than the apparently default alphabetical order. I've got a simple model:

public class Application : EntityBase
{
    [Display(Name = "Naam", Order = 1)]
    public string Name { get; set; }

    [Display(Name = "Omschrijving", Order = 2)]
    public string Description { get; set; }
}

我希望脚手架为Description之前的Name生成一个字段.该怎么做?

I want the scaffolder to generate a field for Name before Description. How to do this?

我一直试图在Razor模板中提出一个解决方案.相关代码为:

I've been trying to come up with a solution in the Razor template. The relevant code is:

...
IEnumerable<PropertyMetadata> properties = Model.ModelMetadata.Properties;
foreach (var property in properties)
{
    if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey)
    {
...

我希望属性具有Order-property,所​​以我可以写类似

I was hoping that a property had an Order-property, so I could write something like

foreach (var property in properties.OrderBy(p => p.Order))

有什么想法吗?

推荐答案

因此,在进行一些(深入)挖掘之后,我想出了一个解决方案.当我已经自定义模板时,可以再添加一个自定义.我最终创建了一个帮助器类ScaffoldHelpers.cs:

So, after some (deep) digging I came up with a solution. As I allready customized the templates, it was acceptable to add yet another customization. I ended up creating a helper class ScaffoldHelpers.cs:

public class ScaffoldHelpers
{
    public static IEnumerable<PropertyMetadata> GetPropertiesInDisplayOrder(string typename, IEnumerable<PropertyMetadata> properties)
    {
        Type type = Type.GetType($"{typename}, {typename.Split('.')[0]}");
        SortedList<string, PropertyMetadata> propertiesList = new SortedList<string, PropertyMetadata>();
        foreach (PropertyMetadata property in properties)
        {
            int order = 0;
            if (type != null)
            {
                var member = type.GetMember(property.PropertyName)[0];
                var displayAttribute = member.GetCustomAttribute<System.ComponentModel.DataAnnotations.DisplayAttribute>();
                if (displayAttribute != null)
                {
                    order = displayAttribute.Order;
                }
            }
            propertiesList.Add($"{order:000} - {property.PropertyName}", property);
        }
        return propertiesList.Values.AsEnumerable();
    }

}

这将遍历所有属性,并确定是否指定了[Display()]属性.如果是这样,它将获得Order-parameter的值.如果未指定,则订购属性将为零.使用SortedList并确保键是按指定顺序排序的,我可以轻松地按所需顺序返回IEnumerable<PropertyMetadata>.

This iterates over all the properties, and determines if a [Display()] attribute is specified. If so, it gets the value of the Order-parameter. If you don't specify this, the Order-property will be zero. Using a SortedList and making sure the key is ordered by the specified order, I'm able to easily return an IEnumerable<PropertyMetadata> in the desired order.

在模板中,我需要为此助手类添加@using.之后,我可以将以下内容插入模板:

In the template, I needed to add @using for this helper-class. After that, I could insert the following into the template:

...
IEnumerable<PropertyMetadata> properties = Model.ModelMetadata.Properties;

// added:
properties = ScaffoldHelpers.GetPropertiesInDisplayOrder(Model.ViewDataTypeName, properties);

foreach (var property in properties)
{
    if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey)
    {
...

就是这样!

这篇关于获取脚手架以特定顺序生成字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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