Inside view I have list of images with corresponding checkboxes. I want to check image or images and store image id's to int array. This int array should be sent to the controller for further process.
I'm already spend too much time on this and I'm getting int[] data null at controller
Question is: Why I'm getting null at controller?
SOLVED! In my _Layout jquery scripts bundle call was at the end of the document, when I was move to the top everything works.
View
<div id="newsImages">
<img width="50" height="50" alt="" src="/imageOne.jpg">
<input type="checkbox" class="imgCheckbox" id="4">
<img width="50" height="50" alt="" src="/imageTwo.jpg">
<input type="checkbox" class="imgCheckbox" id="5">
<input type="button" value="Delete" name="deleteImgBtn" id="deleteImgBtn" class="deleteImagesBtn">
</div>
JS
var imgList = [];
$(document).on("click", "#deleteImgBtn", function (e) {
e.preventDefault();
$('.imgCheckbox:checked').each(function () {
var id = $(this).attr('id');
//add image id to the array of ints
imgList.push(id);
});
jQuery.ajaxSettings.traditional = true;
var options = {
url: '/news/deleteimages',
type: 'POST',
data: { data: imgList },
traditional: true
};
$.ajax(options).done(function (data) {
var $target = $('#newsImages');
$target.html(data);
});
//reset array of int to prevent browser to send duplicated
//img id to the controller on second attempt after ajax request is completed
imgList.length = 0;
//prevent browser from any default actions
return false;
});
CONTROLLER
public ActionResult DeleteImages(int[] data)
{
...
}