Ajax序列化表单为Json字符串POST到后台

$.fn.serializeObject = function () {
                var o = {};
                var a = this.serializeArray();
                $.each(a, function () {
                    if (o[this.name] !== undefined) {
                        if (!o[this.name].push) {
                            o[this.name] = [o[this.name]];
                        }
                        o[this.name].push(this.value || '');
                    } else {
                        o[this.name] = this.value || '';
                    }
                });
                return o;
            };

$('input.pass').mouseup(function () {
                alert(JSON.stringify($(this).parent().parent().serializeObject()) + '
' + typeof (JSON.stringify($(this).parent().parent().serializeObject())));

                $.post('@Url.Action("CheckPartial", "Agent")', JSON.stringify($(this).parent().parent().serializeObject()))
                    .done(function (data) {
                        $(this).parents('div.custom-card').load(data);
                    })
            });




[HttpPost]
public ActionResult CheckPartial()//直接加参数获取不到POST过来的数据
{
Session["Id"] = 1;
var UserID = Convert.ToInt32(Session["Id"]);


using (var streamReader = new StreamReader(Request.InputStream))
{
var checkObject = JsonConvert.DeserializeObject<CheckPartial>(streamReader.ReadToEnd());


using (HoldenZhongbaoEntities _context = new HoldenZhongbaoEntities())
{
//var checkObject = JsonConvert.DeserializeObject(checkJson);
var checkOrderID = checkObject.OrderID as string;


var order = _context
.Cashes
.Where(u => u.OrderID == checkOrderID)
.FirstOrDefault();


order.IsAdminCheck = checkObject.IsAdminCheck;
order.AdminCheckTime = DateTime.Now;
order.Note = checkObject.Note as string;


_context.SaveChanges();


return PartialView("CheckPartial", order);
}
}
}

 

这里

.done(function (data) {
                        $(this).parents('div.custom-card').load(data);
用的不对,load的参数是要发送GET请求时顺带的,局部刷新看下一篇
原文地址:https://www.cnblogs.com/Jayesslee/p/9244755.html