MVC3阿贾克斯后复式复选框值控制器控制器、复选框、阿贾克斯后

2023-09-10 20:07:22 作者:梦如初

使用MVC3我想实现以下。我有有许多值的表,用ajax我希望能够选择拆除复选框(适用于任意数量的行),这是该表的一部分。

Using MVC3 I am trying to accomplish the following. I have a table which has a number of values, using ajax I want to be able to select the remove checkbox (for any number of rows) which is part of the table.

采用Ajax后,结果到控制器进行处理。这个问题我现在已经是我不能让控制器接受来自阿贾克斯后传来的数据。

The using ajax post the result to the controller for processing. The issue I currently have is I cannot get the controller to accept the data coming from the ajax post.

我正在使用jQuery和JSON,使Ajax调用。

I am making use of jQuery and json to make the ajax call.

function removeRooms() {
    var jdata = { 'mRooms': [] };
    $('input:checked').each(function () {
        jdata['mRooms'].push($(this).val());        })

    $.ajax({
        url: "/room/removeselectedroom/@Model.mRoomid",
        type: "POST",
        dataType: "json",
        data: jdata,
        success: function (msg) {
            $('#mRooms').html(msg);
        }
    })
}

任何帮助,您可以给或资源将接入点preciated。

Any help you can give or resources would be appreciated.

:一额外的事情。我通过一个IEnumerable的对象中获取,但有一个参数列表中有上奇怪的价值位置的一个项目,因为只有数字在我的复选框。

: One extra thing. I am getting through a IEnumerable object but there is a parameter an item in position one of the list with the value 'on' strange as there is only numbers in my checkboxes.

推荐答案

尝试将数据序列化传统:真正的

$.ajax({
    url: "/room/removeselectedroom/@Model.mRoomid",
    type: "POST",
    dataType: "json",
    data: jdata,
    traditional: true,
    success: function (msg) {
        $('#mRooms').html(msg);
    }
})

这里了解数组序列的一些信息。

Here some information about array serialization.

对于jQuery 1.4,在$ .PARAM()方法序列化对象的深递归,以适应现代脚本语言和框架,比如PHP和Ruby on Rails。您可以通过设置jQuery.ajaxSettings.traditional =真在全球禁用此功能。

As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;.

ASP.Net MVC模型绑定需要的阵列形式的数据。

ASP.Net MVC model binding requires array data in form of

mRooms=1&mRooms=2&mRooms=3

但是从jQuery的1.4发送

But from jQuery 1.4 it sends

mRooms[]=1&mRooms[]=2&mRooms[]=3

这就是为什么你必须使用传统:真正的

This is why you have to use traditional: true