I need to loop through a number of divs, build and int array and sent it to a controller which accepts (int[] my array), but I carn't figure out how to build the int array to add to the end of the url to send to the controller
$.post('@(Url.Action("VariationInsert", "Item"))', { 'paIDs[]': list}, function (html) {
$(this).closest("#Variations").append(html);
});
};
I would like to post the info if possible, I have checked and the loops works the only thing that isn't working is the receiving in the controller, which is
public PartialViewResult VariationInsert(int[] paIDs)
{
//do something
}
EnenDaveyBoy
Participant
1465 Points
1147 Posts
send int array to controller
Feb 18, 2012 02:52 AM|LINK
HI
I need to loop through a number of divs, build and int array and sent it to a controller which accepts (int[] my array), but I carn't figure out how to build the int array to add to the end of the url to send to the controller
bruce (sqlwo...
All-Star
36882 Points
5451 Posts
Re: send int array to controller
Feb 18, 2012 03:39 AM|LINK
normally you would post the data, but a url query string is the same format;
ActionResult MyAction(int[] myArray)
{
}
javascript:
var a = [1,2,3,4,5];
var query = "?";
for (var i=0; i < a.length; ++i) {
query+= "myArray=" + a[i] + "&";
}
// query will be: ?myArray=1&myArray=2&myArray=3&myArray=4&myArray=5&
abiruban
All-Star
16072 Points
2736 Posts
Re: send int array to controller
Feb 18, 2012 12:49 PM|LINK
Hi
http://stackoverflow.com/questions/7998629/passing-an-integer-array-to-an-mvc-controller-via-ajax
http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form
Thanks...
***DON'T FORGET TO CLICK “MARK AS ANSWER” ON THE POST IF HELPED YOU.
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Re: send int array to controller
Feb 19, 2012 08:54 PM|LINK
thanks but I am not getting anywhere fast
$("#AddVariation").live('click', addVariation);
function addVariation() {
var list = [];
$('#ChildAttributes .formwrapper').each(function () {
list.push($(this).attr('id').replace("ParentAttribute_", ""));
});
$.post('@(Url.Action("VariationInsert", "Item"))', { 'paIDs[]': list}, function (html) {
$(this).closest("#Variations").append(html);
});
};
I would like to post the info if possible, I have checked and the loops works the only thing that isn't working is the receiving in the controller, which is
public PartialViewResult VariationInsert(int[] paIDs)
{
//do something
}
Young Yang -...
All-Star
21343 Points
1818 Posts
Microsoft
Re: send int array to controller
Feb 20, 2012 02:45 AM|LINK
Hi
See this:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Re: send int array to controller
Feb 21, 2012 12:43 AM|LINK
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Re: send int array to controller
Feb 21, 2012 12:44 AM|LINK
got it $.param({ paIDs: list }, true)
you need the ,true other wise there are special charaters entered in the array (no idea why), and the ture doesn't add them.