如何在aspnet core mvc中删除数据后用jquery延迟3000秒重定向页面
我在asp.net core中有一个删除方法。当用户删除数据时,它返回重定向到操作。我想在用户删除数据时删除显示的成功消息,并在3秒钟后重定向另一页如何在aspnet core mvc中删除数据后用jquery延迟3000秒重定向页面,jquery,model-view-controller,Jquery,Model View Controller,我在asp.net core中有一个删除方法。当用户删除数据时,它返回重定向到操作。我想在用户删除数据时删除显示的成功消息,并在3秒钟后重定向另一页 [HttpPost] [Route("Student/Delete/{id:int}")] public IActionResult DeleteConfirmed(int id) { var student = _context.Students.Singl
[HttpPost]
[Route("Student/Delete/{id:int}")]
public IActionResult DeleteConfirmed(int id)
{
var student = _context.Students.SingleOrDefault(s => s.StudentID == id);
if (student == null)
{
return NotFound();
}
_context.Students.Remove(student);
_context.SaveChanges();
return RedirectToAction("Index");
}
您的代码需要在两个位置进行更改:
[HttpPost]
[Route(“Student/Delete/{id:int}”)]
公共IActionResult DELETE已确认(内部id)
{
var student=\u context.Students.SingleOrDefault(s=>s.StudentID==id);
如果(学生==null)
{
返回NotFound();
}
_context.student.Remove(学生);
_SaveChanges();
返回Ok();
}
function delete(id) {
$.post('/Student/Delete/' + id).done(function(result) {
// show your message in some HTML tag with id="message"
$('#message').text('Deleted successfully!');
// wait for 3 seconds before redirecting
setTimeout(() => {
window.location.href = '/Students'; // or wherever to
}, 3000);
}).fail(function(err) {
const msg = 'An error occured';
console.error(msg, err);
$('#message').text(msg);
});
}