防止在ASP.NET中删除记录后出现后退按钮或显示错误
本文关键字:错误 显示 按钮 记录 ASP NET 删除 | 更新日期: 2023-09-27 17:49:59
我有一个ASP。. NET MVC应用程序。
如果用户在浏览器中单击后退按钮,如何显示错误消息而不是返回视图?
例如,我有一个功能,用户可以删除日志。删除后,控制器方法将它们重定向到一个页面。这都很好。
但是,如果用户在被重定向后单击后退按钮,他们将返回到显示已删除记录的同一页面。那么,每次单击后退按钮时,如何验证以检查记录是否仍然存在呢?
关闭日志的代码是:
public ActionResult CloseLog(int id)
{
var query = from log in db.Logs where log.LogID == id select log;
foreach (Log log in query)
{
log.DateTimeResolved = System.DateTime.Now;
Response response = new Response
{
ResponseText = "This log is now closed. Please call the Service Desk on XXXX if needed.",
UserName = User.Identity.Name,
DateTimeOfUpdate = System.DateTime.Now,
LogID = log.LogID
};
db.Responses.Add(response);
}
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
和我的代码查看
public ActionResult CreateServiceRequestResponse(int? id)
{
if (id.HasValue)
{
LogID = id.Value;
}
var Responses = (from a in db.Responses where a.LogID == LogID select new ResponseViewModel { ResponseText = a.ResponseText, DateTimeOfUpdate = a.DateTimeOfUpdate, Username = a.UserName });
var ServiceRequest = (from a in db.ServiceRequests where a.LogID == LogID select new ServiceRequestViewModel { RequestType = a.RequestType, AdditionalInformation = a.AdditionalInformation, EstimatedResolveDate = a.EstimatedResolveDate, LogID = a.LogID});
ViewBag.ServiceRequestData = ServiceRequest;
ViewBag.Data = Responses;
return View();
}
把它放在你认为应该进行验证的地方
var query = from log in db.Logs where log.LogID == id select log;
if (!query.Any())
{
// Or you can have your own handler.
throw new HttpException(404, "Page not found");
}