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;
 }
5
  • 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? Commented Jan 12, 2012 at 9:43
  • maybe you can take a look at this: stackoverflow.com/questions/536283/…. Commented Jan 12, 2012 at 9: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. Commented Jan 12, 2012 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. Commented Jan 13, 2012 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 Commented Jan 13, 2012 at 12:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

guys, for what i was doing this is to delete multiple selction checkbox and pass the string to json .....now i done this..
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..
Have you try a return RedirectToAction("yourAction", "yourController"); instead of returning your result?
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; 
} 

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.