ASP.NET MVC Jquery Ajax发布表单序列化吗? [英] ASP.NET MVC Jquery Ajax post form serialize?

查看:66
本文介绍了ASP.NET MVC Jquery Ajax发布表单序列化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ajax函数

$(function () {
    $('form').submit(function () {
        if ($(this).valid()) {
            $.ajax({
                url: this.action,
                type: this.method,
                data: { model: $(this).serialize(), locations: getCheckedLocation(), reports: getCheckedReports() },
                beforeSend: function () {

                },
                complete: function () {

                },
                success: function (result) {
                    $('#user_operations_container').html(result);
                    setTimeout(function () { LoadAction('@Url.Action("GetAllUsers", "User")') }, 1000);
                    $("#widgets ul li a").removeClass("link_active");
                    $("#widgets ul li:first-child a").addClass("link_active");
                }
            });
        }
        return false;
    });
});

ajax数据属性中使用的功能

functions that are using in ajax data attribute

function getCheckedLocation() {
    var nodes = $('#tt_location').tree('getChecked');
    var s = '';
    for (var i = 0; i < nodes.length; i++) {
        if (s != '') s += ',';
        s += nodes[i].text;
    }
    return s;
}

function getCheckedReports() {
    var nodes = $('#tt_reports').tree('getChecked');
    var s = '';
    for (var i = 0; i < nodes.length; i++) {
        if (s != '') s += ',';
        s += nodes[i].text;
    }

    return s;
}  

HTML

<div> // there are html helpers for model (dropdownlistfor, textboxfor,...)
</div>
<div> // checkbox tree (#tt_location)
</div>
<div> // checkbox tree (#tt_reports)
</div>

控制器

[HttpPost]
public ActionResult _EditUser(UserViewModel model,string locations,string reports)
{
    // model = null
    // locations and reports are expected. (not null)
}

问题

为什么模型为空? 当我使用像这样的ajax数据属性= data: $(this).serialize(),时,它的模型不为null.

Why model is null? When I use ajax data attribute like this = data: $(this).serialize(), , It works model is not null.

如何发布带有其他数据(位置,报告)的模型.

How can I post model, with additional data (locations,reports).

我希望我能解释.谢谢...

I hope I can explain. Thanks...

推荐答案

尝试如下:

 data:$('this').serialize() + "&locations=" + getCheckedLocation() "&reports=" + getCheckedReports() 

它将起作用.

希望有帮助

这篇关于ASP.NET MVC Jquery Ajax发布表单序列化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆