Datasource posting model collection property incorrectly

5 posts, 0 answers
  1. Brett
    Brett avatar
    25 posts
    Member since:
    Sep 2012

    Posted 06 Dec 2012 Link to this post

    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?
  2. Brett
    Brett avatar
    25 posts
    Member since:
    Sep 2012

    Posted 07 Dec 2012 Link to this post

    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.
  3. Daniel
    Admin
    Daniel avatar
    2230 posts

    Posted 10 Dec 2012 Link to this post

    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!
  4. Robert
    Robert avatar
    1 posts
    Member since:
    Oct 2012

    Posted 31 Jan 2013 Link to this post

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

  5. Daniel
    Admin
    Daniel avatar
    2230 posts

    Posted 04 Feb 2013 Link to this post

    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!
Back to Top