Javascript 在MVC5控制器方法中使用json

Javascript 在MVC5控制器方法中使用json,javascript,jquery,ajax,json,asp.net-mvc,Javascript,Jquery,Ajax,Json,Asp.net Mvc,我的Javascript代码。 这将获得复选框的Id,我想将此数据发送到我的控制器,以便我可以使用它从数据库中选择这些特定值。这是我第一次使用Jquery和Ajax,我已经成功地做到了这一点,但我仍然迷路了 var checkboxes = $("input[type='checkbox']"); $(function () { function getValueUsingClass() { /* declare an checkbox array */

我的Javascript代码。 这将获得复选框的Id,我想将此数据发送到我的控制器,以便我可以使用它从数据库中选择这些特定值。这是我第一次使用Jquery和Ajax,我已经成功地做到了这一点,但我仍然迷路了

var checkboxes = $("input[type='checkbox']");
$(function ()
{
    function getValueUsingClass() {
        /* declare an checkbox array */
        var chkArray = [];

        /* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
        $(".chk:checked").each(function () {
            chkArray.push($(this).val());
        });

        /* we join the array separated by the comma */
        var selected = chkArray.join(",") + ",";

        /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
        if (selected.length > 1)
        {
            $.ajax({
                url: "/StateController/MyResult",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                traditional: true,
                type: "POST",
                data: {"randstuff": chkArray},
                success: function () {
                    // do things upon success
                },
                error: function () {
                    alert("Error!");
                }
            });
        }
         else
         {
            alert("Please check at least one of the checkbox");
         }        
    }

    $("#Charter").click(function () {
        getValueUsingClass();
    });
});

我知道我应该有一个这样的方法来接收来自脚本文件的数据,但我真的不知道该怎么做。我想从接收到的Json数据中创建一个数组,然后我可以使用该数组在数据库中循环,并只选择数组中具有匹配ID的值。

我认为您的代码中有一些额外的漏洞,可以进行清理。让我们看一下GETValueUsCurn类,参见代码本身中的注释:

public JsonResult MyResult(int[] randstuff)
        {
            return Json();
        }
现在,MVC应该能够将JSON请求反序列化到控制器方法中的int[]数组中,只要它允许通过HttpPostAttribute进行发布:


这个代码有效吗?您是否收到MyResult方法中的复选框值数组?或者,这个问题是真的吗?我现在有了我的价值观,我如何使用它们来更新我的数据库?我想你实际上是对的。。。。我有我的值,我的问题是将它们传递给我的控制器,你是说我可以做一些像var selectedlist=randstuff这样的事情来获取数组,然后像return Jsonselectedlist那样生成return语句@lagfvu:您可以返回Jsonrandstuff;-。NET将把randstuff序列化回JSON数组。如果您不需要了解有关UI中数组的更多信息,则无需将其返回。使用这些值更新数据库并返回Jsonnew{success=true},JsonRequestBehavior.AllowGet;在done:.donefunctionresult{if result.success==true{alert'success!';}@Cory中,我只需要包含值的数组,这样我就可以使用它们从数据库中选择项目。我不需要对数据库做任何更改。
function getValueUsingClass() {
    // no need for looping and building the array manually, we've got .map() for that
    var chkArray = $('.chk:checked').map(function() {
        return this.value;
    }).get(); // turn into basic array

    // check if the chkArray has any elements, don't build a string
    if (chkArray.length > 1) {
        $.ajax({
            url: '@Url.Action("MyResult", "StateController")', // let MVC build URL for you
            contentType: "application/json; charset=utf-8", // type sending
            dataType: "json", // type receiving
            // traditional: true, // not necessary
            type: "POST",
            data: JSON.stringify({ 'randstuff': chkArray }), // serialization
        }).done(function () { // use jQuery promises
            // do things upon success
        }).fail(function () { // use jQuery promises
            alert("Error!");
        });
    } else {
        alert("Please check at least one of the checkbox");
    }        
}
[HttpPost]
public JsonResult MyResult(int[] randstuff)
{
    return Json();
}