Kendo Grid Inline MultiSelect - 发布值

时间:2016-08-18 22:13:50

标签: kendo-grid kendo-multiselect kendo-editor

我正在复制功能,非常接近这里所见。 https://onabai.wordpress.com/2013/07/17/kendoui-multiselect-in-a-grid-yes-we-can/

我有一个带有内联多选编辑器字段的Kendo网格。我有一个datasource.sync()事件在更改多选时启动。我遇到的问题是如何在后期变量中安排数据。

我在FireFox中使用FireBug。我可以设置一个函数来在我的multiselect字段中查看sync()事件中的值。

console.log(this.value());

这将是我称为“RoleCode”的字符串数组字段。无论如何,控制台日志会按原样显示值,例如

A, OU

但是,当我查看对我的控制器的Post调用和参数时,我看到RoleCode字段是重复的,这就是我的控制器无法识别方法签名的原因。例如,这就是我在FireBug中看到的......

ID  123
Field1  TextFromField1
RoleCode[1][RoleCode]  OU
RoleCode[]  A

知道如何设置它以便post参数可用吗?

更新

现在我只是更改了更新功能,将多选值发送为逗号分隔的字符串。我可以在控制器中处理它们。我真的不喜欢这种设置,但在找到如何正确发送已发布的值之前,我就是这样。

    update: {
            url: "Home/GridUpdate",
            type: "POST",
            dataType: "json",
            data: function () {
                //Grid does not post multiselect array correctly, need to convert to a string
                var rolesString = $("#gridRoleList").data("kendoMultiSelect").value().toString();
                return { rolesString: rolesString };
            },
            complete: function (e) {
                setTimeout(function () {
                    refreshGrid();
                }, 300);
            },
            success: function (result) {
                // notify the data source that the request succeeded
                options.success(result);
            },
            error: function (result) {
                // notify the data source that the request failed
                options.error(result);
            }
        },

更新2

实际上这不是一个好主意,因为如果我在网格中编辑另一个字段,我会收到一个js错误,因为找不到多重选择。

2 个答案:

答案 0 :(得分:0)

看起来您的问题可以通过序列化后发送数据来解决

阅读操作 - 使用MVC Wrapper

.Create(create => create.Action("Create", "Home").Data("serialize"))

JS代码

<script type="text/javascript">

function serialize(data) {
    debugger;
    for (var property in data) {
        if ($.isArray(data[property])) {
            serializeArray(property, data[property], data);
        }
    }
}

function serializeArray(prefix, array, result) {
    for (var i = 0; i < array.length; i++) {
        if ($.isPlainObject(array[i])) {
            for (var property in array[i]) {
                result[prefix + "[" + i + "]." + property] = array[i][property];
            }
        }
        else {
            result[prefix + "[" + i + "]"] = array[i];
        }
    }
}


</script>

Please refer here for complete source code

答案 1 :(得分:0)

这是我如何解决它。在编辑器功能的更改事件中,我使用multiselect的值更新了模型的值。然后数据正确地发布为像这样的字符串数组。

ID  123
Field1  TextFromField1
RoleCode[]  A
RoleCode[]  OU

我的网格编辑器功能

function roleMultiSelectEditor(container, options) {
    $('<input id = "gridRoleList" name="' + options.field + '"/>')
        .appendTo(container)
        .kendoMultiSelect({
            dataTextField: "RoleCode",
            dataValueField: "RoleCode",
            autoBind: true,    
            change: function (e) {
                //Applies the value of the multiselect to the model.RoleCode field
                //Necessary to correctly post values to controller
                options.model.RoleCode = this.value();
                processGridMultiSelectChange();
            },
            dataSource: {
                type: "json",
                transport: {
                    read: {
                        dataType: "json",
                        url: baseUrl + "api/DropDownData/RoleList",
                    },
                }
            },
            dataBound: function (e) {
            }
        });
}
相关问题