Friday, 31 October 2014

Delete multiple rows using checkbox in with jquery mvc

$('#DeleteAllButtonId').click(function () {
    if ($('.chkNumber:checked').length) {
        var chkIds = "";
        $('.chkNumber:checkbox:checked').each(function () {
            chkIds += $(this).val()+",";
        });
        chkIds = chkIds.slice(0, -1).toString();
        var confirmvalue = confirm("Are you sure you want to delete selected ?");
            if (confirmvalue) {
                $.ajax({
                    url: "/Admin/DeleteMultipleBanner",
                    type: "GET",
                    data: { 'sDeleteId': chkIds },
                    success: function (data) {
                            window.location = '/Admin/Banner';
                        },
                     error: function (xhr, status, error) {
                             alert('Server Error');
                        }
                    });
                }
            }
    else {
        alert('Please Select at lease one Checkbox!');
    }                                 
});

[HttpGet]
public void DeleteMultipleBanner(string sDeleteId)
{
   
    List<int> listDeleteId = sDeleteId.Split(',').Select(int.Parse).ToList();
    // In listDeleteId you will get list of integer
    // your table name and delete query
    db.BannerMasters.Where(x => listDeleteId.Contains(x.BannerId)).ToList().ForEach(item =>     db.BannerMasters.Remove(item));
    db.SaveChanges();
}