如何通过Ajax jQuery将列表字符串从视图传递到控制器 [英] how to pass list string from view to controller by ajax jquery

查看:71
本文介绍了如何通过Ajax jQuery将列表字符串从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的html

<ul class="sb_dropdown" style="display:none;">
                    <li class="sb_filter">Chon the loai</li>
                    <li><input type="checkbox" value="All"/><label for="all"><strong>Tất cả</strong></label></li>
                    <li><input type="checkbox" value="Woman"/><label for="Automotive">Đồ nữ</label></li>
                    <li><input type="checkbox" value="Shoes"/><label for="Baby">Giày</label></li>
                    <li><input type="checkbox" value="Bag"/><label for="Beauty">Túi sách</label></li>
                    <li><input type="checkbox" value="Man"/><label for="Books">Đồ nam</label></li>                      
                </ul>

这是我调用控制的ajax,

this is my ajax to call control,

 <script>
                        $('.sb_search').click(function () {
                            var list = [];
                            $('ul.sb_dropdown').find("input:checkbox:checked").each(function () {
                                list.push($(this).val());
                            });
                            var key = { listkey: list };
                            $.ajax({
                                url: '@Url.Action("Search", "Result")',
                                traditional: true,
                                data: list,
                                dataType: "html",
                                type: 'POST',
                                success: function (data) {
                                    alert("success");
                                },
                                error: function () {
                                    alert("fail");
                                }
                            });

                        });
                    </script>

在我的控制器中,我有一个希望通过单击按钮搜索从视图中接收到的参数列表键

In my controller,i have a paramater listkey that i hope will receive from view when i click button search

public ActionResult Result()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Result(List<string> listkey)
    {
        var n = listkey;
        return View();
    }

当我调试时这不是操作结果,它会警告失败.告诉我我在做什么错.请向我提供有关returnjson的信息,为什么我需要使用正常的视图来显示我的结果

when i debug this isn't do the action result,and it alert fail.tell me what im doing wrong.and please help me about returnjson why i need to use instead i want to use normal View to show my result

我已经解决了这个问题,因为我在ajax中放置了错误的动作和控制器.谢谢大家

I has been solved this problem because i put wrong action and controller in my ajax.Thank you all

推荐答案

编辑尝试一下,创建数组并将其传递给控制器​​

Edit try this , create array and pass that to your controller

    var stringArray = new Array();
    stringArray[0] = "item1";
    stringArray[1] = "item2";
    stringArray[2] = "item3";
    var postData = { listkey: stringArray };

比您的数据要多,在您的ajax调用中

than you data will be , in you ajax call

   data: postData 


$.ajax({
        type: "POST",
        url: '@Url.Action("Search", "Result")',
        data: postData,
        success: function(data){
            alert(data.Result);
        },
        dataType: "json",
        traditional: true
    });


您可以这样做,


you can do like this ,

  1. 将您的列表转换成json字符串,如下所示

您的数据将是 data:'{"listkey":'+ JSON.stringify(list)+'}',

$.ajax({
                                url: '@Url.Action("Search", "Result")',
                                traditional: true,
                                 data: '{ "listkey":' + JSON.stringify(list) + '}',
                                dataType: "html",
                                type: 'POST',
                                success: function (data) {
                                    alert("success");
                                },
                                error: function () {
                                    alert("fail");
                                }
                            });

比试图看到您正在获得想要或不想要的结果

than try to see you are getting result you want or not

  [HttpPost]
        public ActionResult Result(List<string> listkey)
        {
            var n = listkey;
            return View();
        }

这篇关于如何通过Ajax jQuery将列表字符串从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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