Wednesday, December 11, 2013

Multiselect in grid edit template.

When setting up a grid with a custom popup edit template, multiselects don't work quite as expected on save. They appear to bind the whole object, not the selected id values.

This snippet should be called as a create/update data formatter:


View
@(Html.Kendo().Grid()
    .DataSource(c => c.Ajax()
        .Create(...).Data("serialize"))
        .Update(...).Data("serialize"))
    ...)

JavaScript

function serialize(data) {
    if (data.FIELD_NAME != null) {
        var selected = data.FIELD_NAME;

        // Remove the old value to prevent binding conflicts.
        delete data.FIELD_NAME;    

        // Select out the values.
        for (var i = 0; i < selected.length; i++) {
            // Rebuild the field in a format friendly to ASP.Net MVC.
            data["FIELD_NAME[" + i + "]"] = selected[i].Value
        }
    }
}

No comments:

Post a Comment