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:
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.