1

I have an aps.net MVC5 web app, I have a controller action post method that looks like this,

   [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Set(int id)
        {
            DbHelper dbHelper = new DbHelper();
            dbHelper.Update<News>("update news set ispublished = 1, publishdate = @PublishDate  where newsid = @NewsId", new { NewsId = id, PublishDate = DateTime.Now });
            return Json(new { success = true, message = "added successfully!", redirectToUrl = Url.Action("NewsList", "Home") }, JsonRequestBehavior.AllowGet);
        }

Now, when I call this method from my View ,

 $(document).on("click", ".set", function () {

        var mId = $(this).attr("data-model-id");
        $.ajax({
            url: "@Url.Action("Set","Home")",
            data: { "id": mId },
            contentType: 'application/json',
            type: 'POST',
            dataType:'json',
            success: function (response) {
                if (response.success) {
                    window.location.href = response.redirectToUrl;
                    dt.ajax.reload();

                    $.notify(data.message, {
                        globalPosition: "top center",
                        className: "success"
                    });
                }
            },
            error: function (response) {
                $.notify(response.message, {
                    globalPosition: "top center",
                    className: "success"
                });
            }
        });
    });

I am always getting this enter image description here

I expect to be redirected to home/newslist url. I tried changing all the parameters of ajax call , adding and removing them, still no matter what I do, I always land at this page.

2 Answers2

0

Try to check after remove the "error" function from your ajax. Also you can try this:

$(document).on("click", ".set", function () {

    var mId = $(this).attr("data-model-id");
    $.ajax({
        url: "@Url.Action("Set","Home")",
        data: { "id": mId },
        contentType: 'application/json',
        type: 'POST',
        dataType:'json',
        success: function (response) {
            if (response.success) {
                window.location.href = response.redirectToUrl;
                dt.ajax.reload();

                $.notify(data.message, {
                    globalPosition: "top center",
                    className: "success"
                });
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $.notify(errorThrown, {
                globalPosition: "top center",
                className: "success"
            });
        }
    });
});

Check if success function is calling or not.

LilFlower
  • 61
  • 6
0

If you need return the full HTML page in ajax response then you need to change Controller method with some changes.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Set(int id)
{
       DbHelper dbHelper = new DbHelper();
       dbHelper.Update<News>("update news set ispublished = 1, publishdate = @PublishDate  where newsid = @NewsId", new { NewsId = id, PublishDate = DateTime.Now });
       //return Json(new { success = true, message = "added successfully!", redirectToUrl = Url.Action("NewsList", "Home") }, JsonRequestBehavior.AllowGet);
       return view("ViewName");
}