Reputation: 865
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.
Upvotes: 0
Views: 931
Reputation: 2052
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.
Upvotes: 1