2016-08-18 60 views
0

我正在复制与此处显示的功能非常接近的功能。 https://onabai.wordpress.com/2013/07/17/kendoui-multiselect-in-a-grid-yes-we-can/Kendo Grid Inline MultiSelect - 发布的值

我有一个内联多选编辑器字段的Kendo网格。我有一个datasource.sync()事件开始改变多选。我遇到的问题是如何将数据安排在post变量中。

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

console.log(this.value());

这将是一个字符串数组场我称之为“RoleCode”。总之,控制台日志显示的值,因为他们应该,例如

A, OU

然而,当我看到中邮呼吁我的控制器,并在参数,我看到了RoleCode场被复制,这是为什么我的控制器不能识别方法签名。例如,这是我在FireBug中看到的...

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

任何想法我应该如何设置它,以便后期参数可用?

UPDATE

现在我只是改变了更新功能来发送多选值作为逗号分隔字符串。我可以在控制器中处理它们。我不太喜欢这种设置,但是直到我发现如何让发布的值正确发送,这就是我要做的。

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错误,因为多选找不到。

回答

0

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

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) { 
      } 
     }); 
} 
0

看起来像你的问题可以通过其序列

读取动作后发送数据来解决 - 使用MVC包装

.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

+0

感谢。我认为这是我需要的,但我没有使用MVC包装。任何想法如何通过datasource.sync()事件上的这些函数或传输的更新函数来运行数据? – madvora

+0

您可以使用transport.read。数据函数从JS发送附加参数,请参考Kendo UI文档。 http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-transport.read.data –