Click here to Skip to main content
14,876,231 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Greetings,

I am learning .Net MVC and I am facing issue in Ajax redirect. In normall pos or get call we can return the user to some specific page like:
C#
return RedirectToAction("AllStudents");
but using json its different or may be I don't know much about it.
I have posted my code in normal and also converted to Ajax.

What I have tried:

Controller code in general:

[HttpGet]
           public ActionResult Delete(int id)
           {
               stude.DeleteStudent(id);
               return RedirectToAction("AllStudents");
           }


and I have use ajax which do the work successfully but it is not redirecting the user to another page, like in the above code it redirects the users to another page.

[HttpGet]
        public JsonResult Delete(int id)
        {
            stude.DeleteStudent(id);
            return Json("true", JsonRequestBehavior.AllowGet);
        }


My JS code: (Edited)

<script>


       function refresh(id) {


           $.ajax({
                method: "POST",
            url: "Delete/" + id,
               success: function (data) {
                   data = JSON.parse(data);
                   console.log(data);

               },
               error: function (err) {
                   console.log('Failed to get data' + err);
               }
           })

       }
   </script>



Action Link:
@Html.ActionLink("Delete", "Delete", new { id = item.Id }, new {onclick = "refresh("+item.Id+");"})


The action is performed and data is deleted but the browser stop like:

Please check Image

Update Result: output[^]

My Aim: I want that the data is deleted without the page refresh, like it normally does. Also please share a link, post, something which shows the page refresh using ajax. like I want to refresh a page after every 1 minute but without reloading it.
Posted
Updated 24-Feb-21 5:51am
v2
Comments
Richard Deeming 24-Feb-21 4:31am
   
Your question is not clear at all. How do you intend to redirect to a different page without reloading the page?

The image suggests that you are submitting a form to the Delete action without using AJAX. Either your function is not being called, or you're not preventing the default action. But since we can't see how your function is wired up, we can't tell you what the problem is.
msz900 24-Feb-21 11:52am
   
please review the updates. Now the data is deleted but it is not staying on the same page, like the screenshot of the output is attached.
j snooze 24-Feb-21 17:14pm
   
Why not have the json return the url of the page you want to redirect to, or if its the same page and you are just trying to refresh it to show that an item has been deleted, then on ajax call success set the current window.location property in javascript to the current page?
msz900 25-Feb-21 11:06am
   
actually, I just want to refresh the Table which contains my data, like without reloading the whole page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900