0

What I tried in my project is like passing checkbox's selected value as a comma separated string to json of my controller.. but i'm not getting a value to the json action.. it shows null over there.

How can I do this? Please help me

function getIds(checkList) 
{
    var idList = new Array();
    var loopCounter = 0;
    jQuery("input[name=" + checkList + "]:checked").each
    (
        function() 
        {
           idList[loopCounter] = jQuery(this).val();
           loopCounter += 1; 
        }
    );
       alert(idList);
          jQuery.getJSON("/Photos/CheckForIdsJson", { idList: idList });     
 }

 [AcceptVerbs(HttpVerbs.Get)]        
 public JsonResult CheckForIdsJson(Int32[] idList)
 {
       JsonResult result = new JsonResult();
        result.Data = idList;
         return result;
 }
amurra
  • 15,221
  • 4
  • 70
  • 87
lakhani
  • 128
  • 2
  • 13
  • Where do you see the null value, in `idList` in you js file, in controller code or in return resul tback in your js code? – Jahan Zinedine Jan 12 '12 at 09:43
  • maybe you can take a look at this: http://stackoverflow.com/questions/536283/jquery-getjson-not-passing-any-values-to-controller. – JoJa Jan 12 '12 at 09:44
  • Put some alerts in your code to evaluate the status of the array, and use Firebug to evaluate the code to determine when/where you are losing your values in the idlist. Once you know where the problem occurs, you can focus on the JSON aspect. – jamesTheProgrammer Jan 12 '12 at 14:47
  • in my controller code.. in javascript where i put the alert , i m geting idlist value but null in controller's action. – lakhani Jan 13 '12 at 10:33
  • uys, for what i was doing this is to delete multiple selction checkbox and pass the string to json .....now i done this.. – Labdhi Lakhani 3 mins ago edit but what i wanna do now like in json method (/Photos/CheckForIdsJson) i delete sme recrds from db as wel as model.. bt i couldnt return view to other view in same controller.. i m doing like after deletion other view called "PhotoList" should b called to list the items with changes done – lakhani Jan 13 '12 at 12:41

2 Answers2

0

You can have a look at this post : AJAX Post of JavaScript String Array to JsonResult as List<string> Always Returns Null? or this one : Send list/array as parameter with jQuery getJson . It seems that you have to indicate traditional: true in your ajax call.

Community
  • 1
  • 1
Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
  • guys, for what i was doing this is to delete multiple selction checkbox and pass the string to json .....now i done this.. – lakhani Jan 13 '12 at 12:36
  • but what i wanna do now like in json method (/Photos/CheckForIdsJson) i delete sme recrds from db as wel as model.. bt i couldnt return view to other view in same controller.. i m doing like after deletion other view called "PhotoList" should b called to list the items with changes done.. – lakhani Jan 13 '12 at 12:39
  • Have you try a `return RedirectToAction("yourAction", "yourController");` instead of returning your result? – Samuel Caillerie Dec 04 '12 at 09:22
0

Use this in your script :

var did ='',
$("#tbl").find("input:checkbox").each(function (i) {
    if (this.checked == true) {
        did += $(this).val() + ",";
    }
});

alert(did); 

if (did == "") {
    alert("Please Select"); 
    return; 
} 
Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
Deepakmahajan
  • 856
  • 1
  • 11
  • 23