Kendo Grid UI Html帮助枚举行值替换

时间:2017-10-02 10:14:43

标签: c# enums telerik telerik-grid telerik-mvc

我有这个例子,我想使用枚举更改带字符串值的数值。我尝试了一些方法,比如.ClientTemplate和.EditorTemplateName。如何使用枚举格式化行?这是一个网格示例:

@(Html.Kendo().Grid<TelerikAspNetCoreApp2.Models.OrderViewModel>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.OrderID).Filterable(false);
                columns.Bound(p => p.Freight).EditorTemplateName("Test"); //This is the value to change
                columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy}");
                columns.Bound(p => p.ShipName).Filterable(f => f.UI("shipName"));
                columns.Bound(p => p.ShipCity).Filterable(f => f.UI("shipCity"));
            })
            .Pageable()
            .Sortable()
            .Scrollable()
            .Navigatable()
            .Pageable()
            .Scrollable()
            .Filterable()
            .HtmlAttributes(new { style = "height:550px;" })
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Read(read => read.Action("Orders_Read", "Grid"))
            )
        ) 
    ....
   @functions {
   public string UseEnum(string x)
   {
       return Enum.Parse(typeof(TelerikAspNetCoreApp2.Enums.Test), x).ToString();
   }
}

这是enum的示例

public enum Test
{
    zero = 0,
    one = 10,
    two = 20,
    three = 30,
    four = 40,
    five = 50
}

public static List<SelectListItem> EnumToSelectList(Type enumType)
        {
            return Enum
              .GetValues(enumType)
              .Cast<int>()
                  .Select(i => new SelectListItem
                  {
                      Value = i.ToString(),
                      Text = Enum.GetName(enumType, i),
                  }
              )
              .ToList();
        }
        @functions {
    public string UseEnum(string x)
    {
        return Enum.Parse(typeof(TelerikAspNetCoreApp2.Enums.Prova), x).ToString();
    }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试在View的View中为该Controller添加一个文件夹,并将其命名为“EditorTemplates”,然后为“Test”创建一个局部视图。这将是您对该字段的控制,并在您使用网格调用该视图时添加 ViewBag.EnumTest ,因为这将映射到您的EditorTemplate部分视图。

然后将其放入“测试”部分视图中。

@(Html.Kendo().ComboBox()
    .Filter("contains")
    .BindTo(ViewBag.EnumTest)
    .Placeholder("Select Freight")
    .HtmlAttributes(new { @id = "Freight", @name = "Freight"})
    .Name("Freight")
)

ViewBag.EnumTest = EnumToSelectList;所以它将有一个将在组合框中使用的SelectListItem。

或者您可以尝试手动填充组合框。

@{
    List<SelectListItem> EnumTest = new List<SelectListItem>
    {
        new SelectListItem 
        {
            Value = "0",
            Text = "zero "
        },
        new SelectListItem 
        {
            Value = "10",
            Text = "one"
        },
        new SelectListItem 
        {
            Value = "20",
            Text = "two"
        },
        new SelectListItem 
        {
            Value = "30",
            Text = "three"
        },
        new SelectListItem 
        {
            Value = "40",
            Text = "four"
        },
        new SelectListItem 
        {
            Value = "50",
            Text = "five"
        }
    };
}

@(Html.Kendo().ComboBox()
    .Filter("contains")
    .BindTo(EnumTest)
    .Placeholder("Select Freight")
    .HtmlAttributes(new { @id = "Freight", @name = "Freight"})
    .Name("Freight")
)