如何将json对象发布到mvc操作

时间:2014-05-22 11:17:28

标签: asp.net-mvc

我有一个mvc动作

[HttpPost]
public ActionResult EditUser(ApplicationUser model)

ApplicationUser类是:

public class ApplicationUser
    {
        public int UserId { get; set; }

        [Required]
        public string UserName { get; set; }

        public byte[] HashedPassword { get; set; }

        public bool IsActive { get; set; }
        public bool IsVerified { get; set; }
}

在jquery方面我正在做:

   $.ajax({
        method: 'POST',
        url: '@Url.Content("~/UserAdmin/EditUser")',
        dataType: "json",
        data:  ....How do I post the object...???
    });

我的问题是当我点击断点时如何将Json对象发布到方法但对象为空。

1 个答案:

答案 0 :(得分:0)

使用JSON.stringify可靠地将JS对象转换为JSON数据,以便通过网络发送

var user = { 
    UserId: '31750',
    UserName: 'chugh97',
    HashedPassword: '...', 
    IsActive: true,
    IsVerified: true
};
...
dataType: 'json',
data: JSON.stringify(user),
...

注意 - 我建议您使用HashedPassword string类型而不是byte[]类型,处理客户端会更容易。