Binding post data to action

2 posts, 1 answers
  1. Gusev
    Gusev avatar
    83 posts
    Member since:
    Dec 2012

    Posted 15 Jun 2013 Link to this post

    Hello! I have a problem with binding MVVM objects to action parameters.

    When I have on client side in viewmodel array of observable objects (for example (pseudocode) array TestArray: 0 {Id: 3, Name: 'qwe', State: 2}, 1 {Id: 5, Name: 'rty', State: 3}) and I post this from popup window (grid editing), on server side I get such form values: TestArray[0][Id], TestArray[0][Name], TestArray[0][State], TestArray[1][Id], TestArray[1][Name], TestArray[1][State]
    But I expect to get: TestArray[0].Id, TestArray[0].Name etc., becouse I want MVC to bind this data to IEnumerable of custom type (public int Id, public string Name, public int State).

    Can you give me advise how to solve this problem, thank tou
  2. Answer
    Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 18 Jun 2013 Link to this post

    Hello,

    How are you posting the data? Could you provide the code you are using? Generally speaking you can convert the data to the format that is expected by the modelbinder for form data as in the snippet:

    function convertData(data, prefix) {
        var result = {};
        for (var i = 0; i < data.length; i++) {
            for (var field in data[i]) {
                result[prefix + "[" + i + "]." + field] = data[i][field];
            }
        }
        return result;
    }
    You could also post it as JSON. Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top