4

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

2
  • Why you want to pass your html to the action result? Instead you can pass only the values which is selected/un-selected. Commented Nov 14, 2014 at 13:34
  • i dont want to pass my html to action result,i want to pass a list value of checkbox where its checked,and i want to open a view of result in search controller Commented Nov 14, 2014 at 13:55

2 Answers 2

6

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(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Its not getting the paramater listkey to my controller when i debug,i think i right when i use ajax to call a action,and pass paramater to its.i dont see any fault on this code
hi prana,i try this and it doesn't work agian.So i do it in the Layout page, i was try to easy change paramater to int id,and i pass it with value 1,it alert fail
@CongLe - pelase do accept answer if it is help you to resolve issue
0
<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(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.