ASP.NET MVC Jquery Ajax发布表单序列化?

Ali*_*hşi 11 asp.net-mvc jquery

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;
    });
});
Run Code Online (Sandbox Code Playgroud)

在ajax数据属性中使用的函数

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;
}  
Run Code Online (Sandbox Code Playgroud)

HTML

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

调节器

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

题

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

如何使用其他数据(位置,报告)发布模型.

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

Kar*_*ala 22

试试这样:

 data:$('this').serialize() + "&locations=" + getCheckedLocation() "&reports=" + getCheckedReports() 
Run Code Online (Sandbox Code Playgroud)

它会工作.

希望能帮助到你