• How to pass list string from view to jquery ajax controller

    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 control the call,

     <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 list of parameters that I hope to get from the view when I click the search button

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

    when i debug this doesnt make the result of the action and it warns fail.tell me what i am 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 solved this problem because I put the wrong action and controller in my ajax. Thank you all

    +3


    source to share


    2 answers


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

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

    what your data will be, you have 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 it,



    • convert the list to json string like below.

    your data will 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 trying to see if you are getting the result you want or not.

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

    +2


    source


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



    0


    source







    All Articles