We are using kendoUI - dataSource to hold objects, that are implemented with composite pattern. Those build up a tree-structure using multiple arrays. Our object looks like this:
Transferring this object from javascript back to MVC3 caused problems on server-side binding.
We found a solution within the kendoUI_TodoList Sample. They used the parameterMap within the transport configuration item on the datasource to map response for MVC3-Databinding:
Nice solution, but it is just working for the first level of the tree. Therefore we came up with a recursive solution, we want to share with you. Just insert the following code snippet inside the transport configuration item of your datasource:
Hopefully this helps someone out there ;-)
best regards,
Reinhard and Stefan
public
class
FilterConstraint
{
public
Guid Id {
get
;
set
; }
public
Operator Op {
get
;
set
; }
public
string
Field {
get
;
set
; }
public
string
Value {
get
;
set
; }
[XmlArray(
"FilterConstraints"
)]
[XmlArrayItem(
"FilterConstraint"
)]
public
List<FilterConstraint> FilterConstraints {
get
;
set
; }
}
Transferring this object from javascript back to MVC3 caused problems on server-side binding.
We found a solution within the kendoUI_TodoList Sample. They used the parameterMap within the transport configuration item on the datasource to map response for MVC3-Databinding:
parameterMap:
function
(data, type) {
if
(type !=
"read"
) {
var
items = {};
$.each(data.models,
function
(index, item) {
for
(
var
key
in
item) {
items[
"["
+ index +
"]"
+
"."
+ key] = item[key];
}
});
return
items;
}
},
Nice solution, but it is just working for the first level of the tree. Therefore we came up with a recursive solution, we want to share with you. Just insert the following code snippet inside the transport configuration item of your datasource:
recursiveParameterMap:
function
(models, items, parentArray, context) {
$.each(models,
function
(index, item) {
for
(
var
key
in
item) {
if
(item[key] !=
null
&& item[key].push) {
context.options.recursiveParameterMap(item[key], items, parentArray +
"["
+ index +
"]"
+
"."
+ key, context);
}
else
{
items[parentArray +
"["
+ index +
"]"
+
"."
+ key] = item[key];
}
}
});
},
parameterMap:
function
(data, type) {
if
(type !=
"read"
) {
var
items = {};
this
.options.recursiveParameterMap(data.models, items,
""
,
this
);
return
items;
}
},
Hopefully this helps someone out there ;-)
best regards,
Reinhard and Stefan