1

I'm trying to create a 15 minute timer that when a user clicks a button to "Check out" or lock a case, it starts a timer that in 15 minutes runs an action to flip a boolean switch in my database that will unlock that case again after the 15 minutes. I'm assuming this needs to be done on my server side code and not in Javascript due to if a person leaves the page that script wouldn't run. I was hoping I could insert something in my action Method that could do this. I've researched, but can't find a definitive answer on how to go about this. Any help would be much appreciated.

 using (Html.BeginForm("CheckoutCase", "Case"))
                {
                    @Html.HiddenFor(x => x.ID)
                    <input type="submit" value="Checkout" name="submitAction" class="btn btn-block alert-success"/>
                }

Controller

[HttpPost]
    public ActionResult CheckoutCase(int id)
    {
        Case currentCase = db.Cases.Find(id);

        currentCase.LockCase = true;
        currentCase.Lockout_TS = DateTime.Now;
        db.SaveChanges();

        string url = this.Request.UrlReferrer.AbsolutePath;
        return Redirect(url);

    }
2
  • 2
    if a case is locked for 15 minutes, is it locked for all users?
    – trashr0x
    Oct 7, 2015 at 22:57
  • That's correct. There is an index page that the user wouldn't be able to click on the link to access that details page.
    – brachen33
    Oct 7, 2015 at 23:27

1 Answer 1

2

Assuming that currentCase.Lockout_TS holds the DateTime by which currentCase was flagged as locked and that when a case is locked it's locked for all users, why not check the elapsed time in minutes between DateTime.Now and currentCase.Lockout_TS using TimeSpan.TotalMinutes?

//if currentCase is locked...
if (currentCase.LockCase)
{
    //...check if it's been more than 15 minutes since currentCase was locked
    bool shouldUnlock = (DateTime.Now - currentCase.Lockout_TS).TotalMinutes > 15;
    //if yes, then unlock it
    if (shouldUnlock)
    {
        currentCase.LockCase = false;
        //persist the changes and proceed accordingly 
    }
    //if we've reached this point, shouldUnlock was false _
    //which means that currentCase shouldn't be unlocked yet _
    //so proceed accordingly        
}    
else
{
    //currentCase is not locked, proceed accordingly
}
2
  • Thanks for the help out. I'm pretty sure I understand what your doing here, but it doesn't like the .TotalMinutes'. I looked on MSDN and I don't see any namespace or anything that I would be missing. Do you know what would cause that to error? Thanks again. this is the error Error 1 'System.Nullable<System.TimeSpan>' does not contain a definition for 'TotalMinutes' and no extension method 'TotalMinutes' accepting a first argument of type 'System.Nullable<System.TimeSpan>' could be found (are you missing a using directive or an assembly reference?)
    – brachen33
    Oct 8, 2015 at 16:32
  • Is currentCase.Lockout_TS a DateTime or a DateTime? (nullable?) If it's nullable, you'll have to pass in it's value (currentCase.Lockout_TS.Value) instead. To verify that the value is not null you can first check if currentCase.Lockout_TS.HasValue returns true, if it does you can safely use currentCase.Lockout_TS.Value
    – trashr0x
    Oct 8, 2015 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.