关于 c#:Dropdown 在 Kendo 网格中绑定到枚举

Dropdown in Kendo grid tied to enum

我在 MVC 中工作,并在我们的前端使用 Kendo。我们有一个具有 Enum 值的模型。在我们的前端,我试图生成一个 Kendo 网格,并且能够从网格中的下拉列表中修改 Enum 的值。

型号:

1
2
3
4
5
public class TankModel:FieldDeviceModel
{
    //Other properties not shown
    public TankLevel Level { get; set; }
}

控制器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class FieldSimulationController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

   //Other methods not shown

    public ActionResult TanksTab()
    {
        return PartialView("Tanks");
    }
}

查看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 @(Html.Kendo().Grid<TankModel>()
  .Name("TanksGrid")
  .Columns(maincolumns =>
  {
          maincolumns.ForeignKey(t => t.Level,
             new SelectList(EnumHelper.GetSelectList(
             typeof(TankLevel)))).EditorTemplateName("ClientLevel").Width(200);
      })
      .HtmlAttributes(new { style ="height: 550px; width:auto; text-align:center" })
      .Sortable()
      .Editable(editable => editable.Mode(GridEditMode.InCell))
      .Pageable(pageable => pageable
          .Refresh(true)
          .PageSizes(true)
          .ButtonCount(5))
      .DataSource(dataSource => dataSource
          .Ajax()
          .Batch(true)
          .Read(read => read
              .Action("Devices_Read","FieldSimulation") // Set the action method which will return the data in JSON format
              .Data("GetTankType") // Specify the JavaScript function which will return the data
          )
          .Update(update => update.Action("Devices_Update","FieldSimulation").Type(HttpVerbs.Post))
          .PageSize(20)
          .Model(model =>
          {
              model.Field(t => t.Level);
          })
      )
    )

现在,我正在使用 Kendo 的 ForeignKey 列类型。这似乎没有给出任何错误,但网格没有显示下拉

我还尝试了 SO 中其他地方的解决方案,例如这里的结果相同,网格中没有出现任何列。

此外,我没有看到任何可以表明问题所在的 JS 错误。

感谢您的时间和任何帮助。


我发现我有两个问题,都很容易被忽视但很容易解决。

我的第一个问题是我的 ForeignKey 列:

1
2
3
maincolumns.ForeignKey(t => t.Level,
             new SelectList(EnumHelper.GetSelectList(
             typeof(TankLevel)))).EditorTemplateName("ClientLevel").Width(200);

首先,我不需要创建 new SelectList,因为 EnumHelper 已经返回了一个。我也从未指定网格应该使用什么作为其显示/值。因此,它根本没有填充该列。为了解决这个问题,我使用了 EnumHelper.

返回的 SelectList 的 ValueText 属性

1
2
3
maincolumns.ForeignKey(t => t.Level,
            EnumHelper.GetSelectList(
            typeof(TankLevel)).ToList(),"Value","Text").Width(200);

我还发现我的项目缺少所有 Kendo EditorTemplates,包括 GridForeignKey 模板。通过将其包含在我的项目中,我能够删除 .EditorTemplateName("ClientLevel") 调用并仅使用默认编辑器模板