ASP.NET MVC4中的JSON

时间:2014-08-03 13:35:51

标签: json asp.net-mvc-4

我有:

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


console.log(JSON.stringify(permissions_JSON));

给了我

[{"id":1,"user_id":2,"project_id":1,"mode":3},{"id":1,"user_id":2,"project_id":2,"mode":3},{"id":1,"user_id":2,"project_id":3,"mode":3},{"id":1,"user_id":2,"project_id":4,"mode":3},{"id":1,"user_id":2,"project_id":5,"mode":3},{"id":1,"user_id":2,"project_id":6,"mode":3},{"id":1,"user_id":2,"project_id":7,"mode":3},{"id":1,"user_id":2,"project_id":8,"mode":3},{"id":1,"user_id":2,"project_id":9,"mode":3}]

服务器端:

[HttpPost]
        public JsonResult Permissions_Set(List<Permission> permissions_JSON)
        {

            foreach (var permission_from_view in permissions_JSON)
            {
                return Json(new { Html = "yes", });
                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();

            return Json(new { Html = "no", });

        }

它总是显示Html =“no”

我不知道为什么它可以遍历permissions_JSON。

请,建议

许可模式:

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; }
    }

或许我可以像JSON一样接收permissions_JSON,而不是List。 但在这种情况下,我不知道如何使用JSON

1 个答案:

答案 0 :(得分:0)

试试这个:

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

[HttpPost]
public JsonResult Permissions_Set(Permission modelval)  <-----------
{......}