So I need to do a couple of things when the user leaves the page. I figure I can capture that event with the JQuery window.unload but I'm not sure how to save some data back into the database.
I'd be happy calling another action or something like that but I don't want to stop the user from going to the page they are headed to.
Any ideas?
UPDATED: Solution
Here's the solution I went with (I'll make the error handling a bit nicer later :)).
Since I need to perform a delete operation, I added some extra security after reading this post; http://stephenwalther.com/blog/archive/2009/01/21/asp.net-mvc-tip-46-ndash-donrsquot-use-delete-links-because.aspx
In the view:
$(window).unload(function() {
var ajurl = "/Assignments/unlockAssignment/" + '@model.ID' + "/";
$.ajaxSetup ({
cache: false
});
$.ajax({
type: "DELETE",
url: ajurl,
error: function (xhr, status, error) {
//do something about the error
alert('error');
},
success: function(){
alert('Data Saved:');
}
});
});
The Controller
<AcceptVerbs(HttpVerbs.Delete)>
<Authorize()>
Function unlockAssignment(ByVal id As Integer)
Dim ass As Assignment = db.Assignments.Find(id)
Dim lck = From a In db.AssignmentLocks
Where a.ID = ass.Lock.ID
Select a
db.Entry(lck.FirstOrDefault).State = EntityState.Deleted
db.SaveChanges()
End Function