This is a migrated thread and some comments may be shown as answers.

Datasource posting model collection property incorrectly

4 Answers 210 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 2
Brett asked on 06 Dec 2012, 03:44 PM
I have run into an issue with the DataSource object of my Grid. In short, I bind the current record in the Grid DataSource to a popup window, make some changes to the model in the view (which makes the record "dirty"), and click a save button which calls the myGrid.dataSource.sync() method.

All is well and good up to the point when my Controller receives the POST on the server-side. My model contains a collection property. The problem is that the model being posted by the DataSource is sending the collection property names in the wrong format and ASP.NET does not know how to bind the data to my model.

Here is an example of my model:
public class Member {
  public Member() {
    this.Relatives = new List<Relative>();
  }
  public string Id { get; set; }
  public string Name { get; set; }
  public ICollection<Relative> Relatives { get; set; }
}
  
public class Relative {
  public string Name { get; set; }
  public int Age { get; set; }
}
Like I mentioned before, everything works as expected, up until the model changes are posted to the server. This is the format the model property names are sent in when received by the server:
Member.Id
Member.Name
Member.Relatives[0][Name]
Member.Relatives[0][Age]
Member.Relatives[1][Name]
Member.Relatives[1][Age]
Member[Id]
Member[Name]
Member[Relatives][0][Name]
Member[Relatives][0][Age]
Member[Relatives][1][Name]
Member[Relatives][1][Age]
This is confusing to me. Why are there brackets around the collection property names? There should be a dot after the index (i.e. Member.Relatives[0].Name). Also, why is the model posted twice and in a more incorrect format?

4 Answers, 1 is accepted

Sort by
0
Brett
Top achievements
Rank 2
answered on 07 Dec 2012, 06:10 PM
I figured out what the problem was...sort of. First thing I did was that instead of using the ASP MVC wrapper, I replaced it with the JavaScript implementation. I then noticed that the content-type header when the Grid posts data to the server was incorrect. Since JSON data is being posted, I needed to change the content-type header to 'application/json'. Second, I figured out that I needed to stringify the JSON payload being sent to the server. So, in the DataSource transport.parameterMap function I checked if the transport type is 'update' and returned JSON.stringify(data). That corrected the problem.

However, I still couldn't figure out why the data was being posted twice to the server when using the ASP MVC wrapper.
0
Daniel
Telerik team
answered on 10 Dec 2012, 01:36 PM
Hello Brett,

The data will not be passed in the format that MVC expects for form data when using the MVC wrappers if the kendo.aspnetmvc file is not included. 

Kind regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Robert
Top achievements
Rank 2
answered on 31 Jan 2013, 06:39 PM
We are running into this exact same issue right now. The aspnetmvc.min.js is included as well. Using the wrapper to build the grid, the source output shows a dataSource type of aspnetmvc-ajax.

snippet of output:
.....
"dataSource":{"transport":{"prefix":"","read":{"url":"/Security/User/GetUser"},"update":{"url":"/Security/User/UpdateUser"},"create":{"url":"/Security/User/CreateUser"}},"pageSize":10,"page":1,"total":0,"serverPaging":true,"serverSorting":true,"serverFiltering":true,"serverGrouping":true,"serverAggregates":true,
"type":"aspnetmvc-ajax".......

Our grid UserModel class has a public collection property ICollection<RoleKeyModel> GlobalRoleKeys;

the sub-model RoleKeyModel class has one property, int RoleKey

This UserModel.GlobalRoleKeys collection is sent to the grid correctly (I see the values in e.model.GlobalRoleKeys within the grid.save event)

However, when posted back on update, the values disappear.

A snippet of the parsed-posted data to the webserver is as follows:

....
GlobalRoleKeys[0][RoleKey]: 1
GlobalRoleKeys[1][RoleKey]: 13
GlobalRoleKeys[2][RoleKey]: 19
....

A break within the MVC Controller Action method UpdateUser, I hover over my returned model, and the collection of GlobalRoleKeys is there, but RoleKey value is 0 in each object... in other words, the values are not being parsed and set to the posted model correctly...

To add fuel to this dilemma, If I use ICollection<int> GlobalRoleKeys instead, GlobalRoleKeys is null.

Any thoughts?

The Controller Action is defined as...
public ActionResult UpdateUser([DataSourceRequest]DataSourceRequest request, UserModel model)
{
}

0
Daniel
Telerik team
answered on 04 Feb 2013, 04:54 PM
Hello Robert,

Currently converting the format of nested arrays is not supported. It is possible to implement this by using the request data function like demonstrated in the snippet below:

.Update(update => update.Action("action", "controller").Data("convertData"))
function convertData(data) {
    for (var i = 0; i < data.GlobalRoleKeys.length; i++) {
        for (var property in data.GlobalRoleKeys[i]) {
            data["GlobalRoleKeys[" + i + "]." + property] = data.GlobalRoleKeys[i][property];
        }
    }
}
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Brett
Top achievements
Rank 2
Answers by
Brett
Top achievements
Rank 2
Daniel
Telerik team
Robert
Top achievements
Rank 2
Share this question
or