剑道MVC网格编辑ENUM列

剑道MVC网格编辑ENUM列

本文介绍了剑道MVC网格编辑ENUM列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型,我需要显示和编辑在剑道的MVC电网的组成部分。

I have a simple model that I need to display and edit in Kendo's MVC Grid component.

public class MyModel
{
    public int Id {get; set;}
    public string SomeProperty {get; set;}
    public MyEnum MyEnum {get; set;}
}

public enum MyEnum
{
    FirstItem = 1,
    SecondItem = 2,
    ThirdItem = 3
}

和我有我的网格设置是这样的:

And I have my grid set up like this:

@(Html.Kendo()
    .Grid<MyModel>()
    .Name("grid").Columns(columns =>
    {
        columns.Bound(o => o.SomeProperty).Width(200);
        columns.Bound(o => o.MyEnum).Width(200);
        columns.Command(command =>
        {
            command.Edit().Text("Edit");
        }).Width(220);
    })
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Batch(false)
        .ServerOperation(false)
        .Model(model =>
        {
            model.Id(p => p.Id);
            model.Field(p => p.SomeProperty);
            model.Field(p => p.MyEnum);

        })
        .Create(...) // removed for confidentiality
        .Read(...)
        .Update(...)
        .Events(...)
    ).Filterable()
  .Sortable())

执行编辑命令后,我收到在定义控制器的方法更新模型更新(...)

public virtual async Task<ActionResult> Update(
        [DataSourceRequest] DataSourceRequest request,
        MyModel myModel)

但无论我做什么,基于myModel 在控制器 MyEnum 的默认值。即使我只更改 SomeProperty MyEnum 设置为 SecondItem ,这将是与firstItem 在控制器中。这是通过观察POST请求验证,所以这个问题是某处的网格,而不是服务器。

However no matter what I do, myModel in controller has default value of MyEnum. Even if I change just SomeProperty of item that has MyEnum set to SecondItem, it will be FirstItem in the controller. This was verified by looking at the POST request, so the problem is somewhere in the Grid, not on server.

如何做到正确的数据绑定,以确保剑道发送正确 MyEnum 值?

How to do proper data binding to ensure Kendo sends correct MyEnum values?

推荐答案

您需要使用 .ForeignKey()在列,而不是绑定:

You need to use .ForeignKey() on a column instead of binding:

@(Html.Kendo()
    .Grid<MyModel>()
    .Name("grid").Columns(columns =>
    {
        columns.ForeignKey(o => o.MyEnum, ).EditorTemplateName("GridForeignKey").Width(200);

        // SNIP

,然后在定义 GridForeignKey.cshtml 查看/共享/ EditorTemplate 是这样的:

@model object

@{
    var selectList = (SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"];
}

@Html.DropDownListFor(m => m, selectList, "Choose...")

这样你会得到您的网格下拉,将得到正确的值到您的POST控制器。

This way you get a dropdown in your grid and will get correct values into your controller on POST.

这篇关于剑道MVC网格编辑ENUM列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:15