1

AJAXでMVC3を使用していました。別の AJAX 内で AJAX を使用していました。最初の AJAX 要求では、データと共にコントローラーに適切に送信されます。しかし、AJAX リクエストから戻った後、同じデータを別のアクションに再度投稿しました。しかし、コントローラーに来ると、2回目にデータがnullになります。この問題を解決するのを手伝ってください。

コード

     $(document).ready(function () {
    $('.deleteitems').live('click', function (e) {
        var items = [];
        $('.checkall').each(function () {
            if ($(this).is(':checked'))
                items.push($(this).attr("Id"));
        });
        var json = JSON.stringify(items);
        var perm = $("#Permission").val();
        if (items.length != 0) {
            if (perm == "True") {

                $.ajax({
                    url: '/ItemControl/ItemControl/Catalogue_Check',
                    type: 'POST',
                    dataType: 'json',
                    data: json,
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        if (data == "S") {

                            DelBibsAlso();

                        }
                        else {

                            DelItemsonly();
                        }

                    }
                });
            }
            else {
                $("#divMes").removeClass("Success");
                $("#divMes").addClass("Error");
                showError("Sorry you dont have Permission!");
            }
        }
        else {
            alert("Select any Items to delete");
            e.preventDefault();
        }
    });
});


function DelItemsonly() {
    var items2 = [];
    $('.checkall').each(function () {
        if ($(this).is(':checked'))
            items2.push($(this).attr("Id"));
    });
    var itemjson = JSON.stringify(items2);
    var val = confirm("Are you sure you want to delete these records?");
    if (val) {
        $.ajax({
            url: '/ItemControl/ItemControl/DeleteItems',
            type: 'POST',
            dataType: 'json',
            data: { "id": itemjson, "DelBib": 0 },
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                e.preventDefault();
                $(".t-grid .t-refresh").trigger('click');
                $("#divMes").removeClass("Error");
                $("#divMes").addClass("Success");
                showError("Data Successfully Deleted");
            }
        });
    }
    else {
        e.preventDefault();
    }
}


function DelBibsAlso() {
    var items1 = [];
    $('.checkall').each(function () {
        if ($(this).is(':checked'))
            items1.push($(this).attr("Id"));
    });
    var bibjson = JSON.stringify(items1);
    var value = confirm("Do you also want to delete the catalogue record?");
    var cond = 0;
    if (value) {
        var cond = 1;
    }
    else {
        cond = 0;
    }
    var val = confirm("Are you sure you want to delete these records?");
    if (val) {
        $.ajax({
            url: '/ItemControl/ItemControl/DeleteItems',
            type: 'POST',
            dataType: 'json',
            data: { "id": bibjson, "DelBib": cond },
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                e.preventDefault();
                $(".t-grid .t-refresh").trigger('click');
                $("#divMes").removeClass("Error");
                $("#divMes").addClass("Success");
                showError("Data Successfully Deleted");
            }
        });
    }
    else {
        e.preventDefault();
    }
}

コントローラーコード

    public ActionResult Catalogue_Check(string[] id)
    {
        DeleteItem weed = new DeleteItem();
        int[] ints = id.Select(x => int.Parse(x)).ToArray();
        var Matched_BibID = (from asd in Db.Items
                             where ints.Contains(asd.Id)
                             select asd.BibId).ToList();

        foreach (var idd in ints)
        {
            var bibid = (from bib in Db.Items where bib.Id == idd select bib.BibId).Single();
            var checkbib = (from bibchk in Db.Items where bibchk.BibId == bibid && !ints.Contains(bibchk.Id) select bibchk.Id).Count();
            if (checkbib == 0)
            {
                return Json("S", JsonRequestBehavior.AllowGet);
            }

        }

        return Json("N", JsonRequestBehavior.AllowGet);

    }

    public JsonResult DeleteItems(string[] id, int? DelBib)
    {
        //var newid = id.Split(',');
        DeleteItem weed = new DeleteItem();
        int[] ints = id.Select(x => int.Parse(x)).ToArray();
        foreach (var a in id)
        {
            int sel = Convert.ToInt32(a);
            Item item = Db.Items.Single(i => i.Id == sel);

            int Sess = Convert.ToInt16(Session["AccId"]);
            string AdminName = User.Identity.Name;
            bool Exist = Db.RouteOuts.Any(itm => itm.ItemId == item.Id);

            if (!Exist)
            {
                weed.DeleteIt(item, Sess, AdminName);
                var bibid = (from bib in Db.Items where bib.Id == item.Id select bib.BibId).Single();
                var checkbib = (from bibchk in Db.Items where bibchk.BibId == bibid && !ints.Contains(bibchk.Id) select bibchk.Id).Count();
                if (checkbib == 0)
                {
                    Db.ExecuteStoreCommand("update Bibs set Status= 'D' where Id={0}", item.BibId);
                    Db.SaveChanges();
                }
            }
        }
        return Json("S", JsonRequestBehavior.AllowGet);
    }
4

1 に答える 1

1
function Test() {
    var items1 = [];
    $('.checkall').each(function () {
        if ($(this).is(':checked'))
            items1.push($(this).attr("Id"));
    });
    //var bibjson = JSON.stringify(items1); **** Loose this line ****
    var value = confirm("Do you also want to delete the catalogue record?");
    var cond = 0;
    if (value) {
        var cond = 1;
    }
    else {
        cond = 0;
    }
    var val = confirm("Are you sure you want to delete these records?");
    if (val) {
        $.ajax({
            url: '/ItemControl/ItemControl/DeleteItems',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify({ "id": items1, "DelBib": cond }), // **** Items are stringified before posting ****
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                e.preventDefault();
                $(".t-grid .t-refresh").trigger('click');
                $("#divMes").removeClass("Error");
                $("#divMes").addClass("Success");
                showError("Data Successfully Deleted");
            }
        });enter code here
    }
    else {
        e.preventDefault();
    }
于 2013-03-25T07:10:37.387 に答える