【问题标题】:JQuery in MVC - Send form data as array with getJsonMVC 中的 JQuery - 使用 getJson 将表单数据作为数组发送
【发布时间】:2015-06-10 00:31:25
【问题描述】:

我有一个 MVC 视图,它通过使用 getJSON 定期指向我的控制器中的方法并解析返回的 Json 来更新页面上的元素。

控制器中的方法蓝图:

public JsonResult GetAttendeeJson()
{

    //Code which creates the Json I need.

    return Json(result, JsonRequestBehavior.AllowGet);
}

从视图调用:

function showDetects() {
            // This is the main AJAX that retrieves the data.
            $.getJSON('/Attendee/CardDetections', function (data) {

                $.each(data, function (i, country) {
                    // perform actions using data.

                });

            });
        }

了解我在做什么并不重要,但我的情况发生了变化,我现在在视图中添加了一个包含可变数量复选框的表单(取决于使用页面的用户,复选框的数量会有所不同) .

复选框表单创建:

<form onsubmit="@Url.Action("Index", "Attendee")" method="post" id="checkboxform">
@{int i = 0;}
@{foreach (string s in ViewBag.LocationNames)
{
    <div class="radio-inline">
        @s
        <input class="checkboxcontrol" onchange="this.form.submit()" type="checkbox" id="CheckBoxes" name="CheckBoxes" value="@ViewBag.LocationIds[i]">
    </div> 

    i++;
}

}

</form>

添加此表单意味着我现在需要返回 Json 的控制器方法才能使用这些复选框的数据。 GetAttendeeJson 现在需要知道表单上当前选中了哪些复选框。

所以我希望方法蓝图是这样的:

 public JsonResult GetAttendeeJson(int[] checkBoxValues)
    {

        //Code which creates the Json I need using the checkbox values.

        return Json(result, JsonRequestBehavior.AllowGet);
    }

是否可以在不提交表单的情况下执行此操作?我使用提交来做其他导致重新加载页面的事情。我使用 getJson 来更新页面内容。

理想情况下,我只想在数组中获取选中复选框的 Value 字段,并在调用时将其作为参数发送到 GetAttendeeJson 函数。

谢谢, JK

【问题讨论】:

    标签: javascript jquery asp.net-mvc checkbox


    【解决方案1】:

    假设您有以下 HTML -

    <input type="checkbox" name="chk" value="1" /> 1
    <input type="checkbox" name="chk" value="2" /> 2
    <input type="checkbox" name="chk" value="3" /> 3
    <input type="checkbox" name="chk" value="4" /> 4
    
    <input type="button" id="submit" value="Submit"/>
    

    然后我们可以使用AJAX POST 将选中的复选框推送到操作方法,如下所示 -

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script>
        $(function () {
            $("#submit").click(function () {
                var vals = [];
                $('input:checkbox[name=chk]:checked').each(function () {
                    vals.push($(this).val());
                });
    
                $.ajax({
                    url: "/Home/GetAttendeeJson",
                    type: "post",
                    dataType: "json",
                    data: JSON.stringify({ checkBoxValues: vals }),
                    contentType: 'application/json; charset=utf-8',
                    success: function (result) {
                        if (result.success) {
                        }
                        else {
                        }
                    }
                });
            });
        });
    </script>
    

    当我们点击按钮时,可以在以下控制器动作中获得选中的复选框 -

     public JsonResult GetAttendeeJson(int[] checkBoxValues)
     {
          //Code which creates the Json I need using the checkbox values.
          return Json("true", JsonRequestBehavior.AllowGet)
     }
    

    查看渲染如下,我们可以选中/取消选中复选框 -

    然后当你下一个断点并调试代码时,输​​出将如下所示 -

    【讨论】:

    • 非常感谢您的详细回复。我不希望它发生在按下按钮时它会运行 5-10 秒,但这很棒。我对 ajax 不是很熟悉,当你执行 post 方法时,视图中的哪个变量会保存我的控制器返回的 json?
    • result 将保存从控制器操作返回的数据。如果您想每 5 秒运行一次代码,请使用 JQuery 的 setTimeout()
    • 太棒了。非常感谢你
    【解决方案2】:

    尝试定期触发 ajax 调用。这样您就可以在不提交表单的情况下将其发送到您的函数。

    【讨论】:

    • 对不起,我已经定期调用它了。我忘了这很重要,因为我的重点是如何将表单数据作为数组发送到我的控制器方法,因为我缺乏 ajax 经验.....Ramiramilu 已经回答了我所需要的。
    猜你喜欢
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多