Click here to Skip to main content
15,063,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
JavaScript
Iam sending the json data through ajax...

    var JSONobj = {
        Group: "FisrtGroup", Employees: [
    { EmpId: "1", FullName: "Doe" },
    { EmpId: "4", FullName: "Jones" }]
    };

when iam sending the request.. its sending data like this...

Group: "FirstGroup"
Employees[0][EmpId] : "1"
Employees[0][FullName] : "Doe"
Employees[1][EmpId] : "2"
Employees[1][FullName] : "Jones"

which not able to bind employees data on ASP.NET MVC Model 
Hence I need to send like this using the dot notation not indexed based..

Group: "FirstGroup"
Employees[0].EmpId : "1"
Employees[0].FullName : "Doe"
Employees[1].EmpId : "2"
Employees[1].FullName : "Jones"

This structure is auto binded to Models in asp.net mvc 


What I have tried:

I have tried to send the the data using the object containing the collection of objects(employee) but nothing helped..any suggestions.
Posted
Updated 12-Apr-16 23:14pm
Comments
John C Rayan 12-Apr-16 5:49am
   
Can you post your code here
John C Rayan 12-Apr-16 9:37am
   
Try with double quotes for fields.
var JSONobj = {
"Group": "FisrtGroup", "Employees": [
{ "EmpId": "1", "FullName": "Doe" },
{ "EmpId": "4", "FullName": "Jones" }]
};
John C Rayan 12-Apr-16 9:39am
   
For numbers, you can ignore the quotes in "1" and "4"
Sinisa Hajnal 13-Apr-16 2:22am
   
I believe this is the solution. JSON is not equal to javascript object. The difference being: properties have to be in quotes too.

Make a class.

Public Class JSONobj
{
public string Group{get;set;}
public List<employees> {get;set;}//List of Employees class
}

Then pass this class object as argument in your WebMethod.
   
v2
Try with double quotes for fields.
JavaScript
var JSONobj = {
"Group": "FisrtGroup", "Employees": [
{ "EmpId": "1", "FullName": "Doe" },
{ "EmpId": "4", "FullName": "Jones" }]
};


For numbers, you can ignore the quotes in "1" and "4"
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900