I need to pass values from javascript to my controller action.
$.getJSON('/gallery/PublishImage', { imageid: itemsarray }, function (mydata) {
});
In javascript, it have a value. At the controller, it is null
public ActionResult PublishImage(string imageid)
{
var mydata = imageid;
return Json(mydata,JsonRequestBehavior.AllowGet);
}
how to resolve this.
my entire code is:
function publish() {
debugger;
var $trash = $("#trash li");
var itemsarray = [];
var lis = document.getElementById("trash").getElementsByTagName("li");
for (var i = 0; i < lis.length; i++) {
var item = lis[i].children[0].id;
itemsarray.push(item);
}
$.getJSON('@Url.Action("PublishImage")', { imageids: itemsarray }, function (mydata) {
});
in controller
public ActionResult PublishImage(string[] imageids)
{
var mydata = imageids;
return Json(mydata,JsonRequestBehavior.AllowGet);
}
for testig used string. but in the above code also return null.
is any thing am missed?
Given the name of this javascript variable I strongly suspect that
itemsarrayis not a string but some kind of javascript object (an array?). It must be string for this to work:If you want to send an array then you could do that:
and your controller action: