Jammer
Jammer

Reputation: 10206

Kendo Grid Not Databinding Prior to calling Update Method

I'm using the Kendo Grid and have a collection of ViewModels being loaded in a Read method. Everything is good here and working as expected.

However, when the save button is clicked on the grid the objects presented to the Update method are no longer complete and usable.

All properties are set as expected except a simple string[] property is always left null.

The Grid is specified like:

@(Html.Kendo().Grid<Occam.Portal.ViewModels.UserViewModel>()
    .Name("SystemUserGrid")
    .Columns(columns =>
        {
            columns.Bound(user => user.UserName);
            columns.Bound(user => user.Email);
            columns.Bound(user => user.Roles):
         }
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model =>
    {
        model.Id(m => m.UserId);
        model.Field(p => p.UserName).Editable(false);
        model.Field(p => p.Roles).Editable(false);
    })

.Read("SystemUsers_Read", "Administration")
.Update("SystemUsers_Update", "Administration")
  ))
...

Why would the Kendo libraries leave the string[] property Roles as null despite the data being correctly bound to the grid?

I cannot find anything related to this on the Kendo demos or forums.

Upvotes: 0

Views: 312

Answers (1)

Trey Gramann
Trey Gramann

Reputation: 2004

I think because it is not passing you back the exact same model you sent, it creates a new UserViewModel List and tried to apply the values to it, and does not know how for string[] since it is not a base type; there are many types not supported. If you alter your model to include RolesStr which is a string and have methods in the model to convert back and forth from string[] to string (using split, etc) then I think you will get what you want. After your controller gets the UserViewModel just call the method to repopulate the Roles.

I would make static methods in your model that can accept the whole List and do them all at once maybe like

public static IList<UserViewModel> SplitEm(IList<UserViewModel> userViewModels)
{
    [Split each RolesStr into Roles]
}
public static IList<UserViewModel> JoinEm(IList<UserViewModel> userViewModels)
{
    [Join all the Roles into each RoleStr]
}

Obviously look at Linq to make these two's implementation trivial.

Upvotes: 2

Related Questions