Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass list string from view to controller by ajax jquery

this is my 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>

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();
    }

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

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

like image 227
Cong Le Avatar asked Nov 15 '25 11:11

Cong Le


1 Answers

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 };

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. convert you list into json string like as below

you data will be 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();
        }
like image 137
Pranay Rana Avatar answered Nov 17 '25 08:11

Pranay Rana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!