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

Binding post data to action

1 Answer 45 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Gusev
Top achievements
Rank 1
Gusev asked on 15 Jun 2013, 02:28 PM
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

1 Answer, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 18 Jun 2013, 02:11 PM
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!
Tags
MVVM
Asked by
Gusev
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or