我在 MVC 工作,并使用 Kendo 作为我们的前端。我们有一个具有枚举值的模型。在我们的前端,我正在尝试生成一个 Kendo 网格并能够从网格中的下拉列表中修改 Enum 的值。
型号:
public class TankModel:FieldDeviceModel
{
//Other properties not shown
public TankLevel Level { get; set; }
}
Controller :
public class FieldSimulationController : Controller
{
public ActionResult Index()
{
return View();
}
//Other methods not shown
public ActionResult TanksTab()
{
return PartialView("Tanks");
}
}
查看:
@(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 中其他地方的解决方案,例如 here与没有列出现在网格中的结果相同。
此外,我没有看到任何表明问题所在的 JS 错误。
感谢您抽出宝贵时间和任何帮助。
最佳答案
我发现我有 2 个问题,这两个问题很容易被忽视但很容易解决。
我的第一个问题是我的 ForeignKey 列:
maincolumns.ForeignKey(t => t.Level,
new SelectList(EnumHelper.GetSelectList(
typeof(TankLevel)))).EditorTemplateName("ClientLevel").Width(200);
首先,我不需要创建一个新的 SelectList
,因为 EnumHelper 已经返回了一个。我也从未指定网格应使用什么作为其显示/值。因此,它根本不会填充该列。为了解决这个问题,我使用了 EnumHelper
返回的 SelectList 的 Value
和 Text
属性。
maincolumns.ForeignKey(t => t.Level,
EnumHelper.GetSelectList(
typeof(TankLevel)).ToList(),"Value","Text").Width(200);
我还发现我的项目缺少所有 Kendo EditorTemplates,包括 GridForeignKey 模板。通过将其包含在我的项目中,我能够删除 .EditorTemplateName("ClientLevel")
调用并仅使用默认编辑器模板
关于c# - Kendo 网格中的下拉列表与枚举相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35585626/