0

I have JSON:

[{"id":1,"user_id":"8","project_id":"1","mode":3},{"id":1,"user_id":"8","project_id":"2","mode":1},{"id":1,"user_id":"8","project_id":"3","mode":1}] 

So, I try to send it to server:

$.ajax({
 url : "User/Permissions_Set",
 dataType: 'json',
 type: 'POST',
 data: JSON.stringify(permissions_JSON),
 success: function (data) {
 console.log(data);
 }
 });

On Serverside:

[HttpPost]
 public void Permissions_Set(List<Permission> permissions_JSON)
 {
 foreach (var permission_from_view in permissions_JSON)
 {
 var permission_from_db = db.Permissions.Where(prm => prm.project_id == permission_from_view.project_id && prm.user_id == permission_from_view.user_id).FirstOrDefault();
 permission_from_db.mode = permission_from_view.mode;
 }
 db.SaveChanges();
 }

But I have error near "foreach": Object reference not set to an instance of an object.

I think problem with List permissions_JSON, but my model "Permission" has properties "id", "user_id", "project_id", "mode"

Please, advice, how I can recieve JSON from client on serverside.

Update:

Now I've changed jquery, and it return successfull result. But anyway I can't work with permissions_JSON. So, is it possible work with permissions_JSON not like "List", but like JSON. Could, you, please anybody advice how to recieve it, and work with it in C#...

Thanks in advance.

asked Aug 1, 2014 at 12:53
7
  • can you show Permission class? Commented Aug 1, 2014 at 12:55
  • public class Permission { public int id { get; set; } public int user_id { get; set; } public int project_id { get; set; } public int mode { get; set; } [ForeignKey("user_id")] public virtual User User { get; set; } [ForeignKey("project_id")] public virtual Project Project { get; set; } } Commented Aug 1, 2014 at 12:56
  • try: data: { permissions_JSON: JSON.stringify(permissions_JSON) } Commented Aug 1, 2014 at 13:00
  • try it in reverse, send a list of items, from server thru ajax to the client javascript. Debug and see what the string looks like, may give you an idea what is wrong or missing. Commented Aug 1, 2014 at 13:13
  • hi, thanks, now error disappear, but now permissions not saved in database, and console.log(data) brings nothing, that means that ajax is not succeed Commented Aug 1, 2014 at 13:33

1 Answer 1

1

Edit: Try doing it this way:

var bob = {permissions_JSON:[{ id: 1, user_id: 8, project_id: 1, mode: 3}]};
request.permissions_JSON = bob;
$.ajax({
 url: "Home/Permissions_Set",
 dataType: 'json',
 type: 'POST',
 contentType: 'application/json;',
 data: JSON.stringify(bob),
 success: function (data) {
 console.log(data);
 }
});

create a new object. Attach the collection to that object. Make sure the collection on the object as the same name as your parameter.

answered Aug 1, 2014 at 13:13

2 Comments

hi, thanks, now error disappear, but now permissions not saved in database, and console.log(data) brings nothing, that means that ajax is not succeed
This should now work 100% with the edits I just made. NOTE: your success methods will never have data as you created a void method. Voids have no return values.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.