简体   繁体   English

Kendo MVC C# 是否可以在某种模板中包含通用字段以轻松包含在所有网格中?

[英]Kendo MVC C# is it possible to have generic fields in some kind of template to include easily in all grids?

So we have about 90 grids.所以我们有大约 90 个网格。 In an attempt to standardize column widths, etc, I was considering pulling out the audit trail fields into some kind of base class but i'm not sure how possible this is.为了标准化列宽等,我正在考虑将审计跟踪字段提取到某种基类中,但我不确定这有多大可能。 We have six fields for create date, last change date, and delete date and who did it, and they are at the end of every grid.我们有六个字段用于创建日期、上次更改日期和删除日期以及是谁做的,它们位于每个网格的末尾。 Is there some kind of way to generate them in one common place and include them?有没有某种方法可以在一个共同的地方生成它们并包含它们?

Yes是的

To solve this, you will need to write an extension method to the GridBuilder class.要解决这个问题,您需要为GridBuilder类编写一个扩展方法

Because you want to add your audit trail columns to the end, you would employ the same strategy that is found in this solution and this solution :由于您想将审计跟踪列添加到最后,您将采用在此解决方案此解决方案中找到的相同策略:

  1. Write extention method .AddAuditTrailColumns()编写扩展方法.AddAuditTrailColumns()
  2. Define your Grid Columns that are non audit trail定义非审计跟踪的网格列
  3. Call .AddAuditTrailColumns()调用.AddAuditTrailColumns()

It would look something like:它看起来像:

public static class Extensions
{
    public static GridBuilder<T> AddAuditTrailColumns<T>(this GridBuilder<T> builder) where T: class
    {
        //add audit trail columns
        builder.Columns(columns =>
        {
            columns.Bound("CreateDate").Filterable(false);
            columns.Bound("CreatedBy");
            columns.Bound("LastChangeDate");
            columns.Bound("LastChangedBy");
            columns.Bound("DeleteDate");
            columns.Bound("DeletedBy");
        });
        return builder;
    }
}

You could use lambdas if all your grids use the same base class.如果所有网格都使用相同的基类,则可以使用 lambda。 In the template:在模板中:

@(Html.Kendo.Grid<Product>("Grid74")
    .BindTo(Model)
    .Columns(columns =>
           {
               columns.Bound(p => p.Name);
               columns.Bound(p => p.Description);
           })
   .AddAuditTrailColumns()
)

I applaud your instinct reusing as much as possible.我为你尽可能重复使用的本能鼓掌。 The people maintaining these 90 grids will have an easier job and you will do less work in the end.维护这 90 个网格的人将有更轻松的工作,最终您会做更少的工作。 WIN-WIN.双赢。

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

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