How to control the action of pressing the previous page on a browser
After testing my application i completed a form with some value , saved then wanted to add another person to the list. but when halfway the form i changed my mind and i press the return button on the browser but then something weird happens. the data from the previous form gets loaded instead of a return to the previous page(should be a list of persons).
How do you prevent this from happening?
Function AddPersoon(subdienstid As Integer) As ActionResult
Dim viewModel As New PersoonViewModel
ViewData("Persoon.rijbewijsId") = ConvertToSelectlistItemList(_iSubDienstService.GetAllRijbewijzen(), 1)
ViewData("Persoon.PersoonStatusId") = ConvertToSelectlistItemList(_iSubDienstService.GetAllPersoonStatussen(), 1)
Return View(viewModel)
End Function
<HttpPost()> _
Function AddPersoon(subdienstid As Integer, model As PersoonViewModel) As ActionResult
If ModelState.IsValid Then
Try
_iSubDienstService开发者_开发技巧.AddPersoonTo(subdienstid, model)
Return RedirectToAction("Details", New With {.id = subdienstid})
Catch ex As Exception
End Try
End If
ViewData("Persoon.rijbewijsId") = ConvertToSelectlistItemList(_iSubDienstService.GetAllRijbewijzen(), model.Persoon.RijbewijsId)
ViewData("Persoon.PersoonStatusId") = ConvertToSelectlistItemList(_iSubDienstService.GetAllPersoonStatussen(), model.Persoon.PersoonStatusId)
Return View(model)
End Function
How to control the action of pressing the previous page on a browser
This is pure javascript, as there are a method that you can hook up called onBeforeUnload
that fires when a user closes or moves to other page in the browser history.
I would recommend that you add a script like (and assuming you're using jQuery):
var msg = "You're about to leave this page, all changes will not been save.\n" +
"Are you sure you want to leave?";
$(window).bind("beforeunload", function () {
if( confirm(msg) )
return true;
return false;
});
Live example.
Hope it helps.
精彩评论