0

Trying to pass an object to the model list from JQuery in following format, but they appear null in VS's debugger

obj.data.GoUsers = [{ Email: 'r@gmail.com', InvitationStatus: 1 }]

Although if I pass in following format it works, but this format doesnt look feasible to build dynamically. Any thoughts?

 for (i = 0; i < 3; i++) {
    obj.data["GoUsers[" + i + "].Email"] = "test" + i + "@g.com";
    obj.data["GoUsers[" + i + "].InvitationStatus"] = i;
 }

GoUsers is the type of list of model.

Public Property GoUsers As List(Of GOUsersModel)

ajax final call

post(obj.url, $.param(obj.data), callback_function)
Ris
  • 1,152
  • 7
  • 29
  • 63

2 Answers2

0

To pass in an array of objects to an MVC controller method, you need to use JSON.stringify.

obj.data.GoUsers = JSON.stringify([{ Email: 'r@gmail.com', InvitationStatus: 1 }, { Email: '123@gmail.com', InvitationStatus: 0 }]);

Read the following post to have a better understanding.

Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • Does this answer your question? Let me know if you need any help @Rishi – Nguyễn Văn Phong Feb 21 '20 at 02:57
  • I have tried using JSON.stringify but didnt work. Any thoughts ? – Ris Feb 21 '20 at 03:32
  • How about `contentType and dataType `? https://stackoverflow.com/a/13255459/9071943 – Nguyễn Văn Phong Feb 21 '20 at 03:33
  • Tried all the options but no luck. Only thing I am uncertain if I need to pass list object as a function parameter to controller or not. For example, My ActionResult function is Public Function Submit(Model As ConfigModel) As ActionResult – Ris Feb 21 '20 at 03:36
0

I think it's your solution.

for (i = 0; i < 3; i++) {
    obj.data["GoUsers"][i]["Email"] = "test" + i + "@g.com";
    obj.data["GoUsers"][i]["InvitationStatus"] = i;
}
Jai Kumaresh
  • 715
  • 1
  • 7
  • 33