Search code examples
asp.net-mvc-4razor-2

MVC4 validation Error Display


I have a MVC 4 view (Razor) that accepts a code sent to a user. They get a number tries to enter it correctly. Each try that is entered wrong they get a validation error "Confirmation Number Incorrect - Please Re-enter" except on the last one, where they exceeded they get a validation error "Too Many Attempts - New Confirmation Number Has Been Sent" then they are immediately redirected to the home page. The problem is there is no delay so the message is never seen.

I really don't want to create another view to post the message with a button to redirect. Is there any way to delay some period of time then redirect the page? Any ideas?


Solution

  • If you are displaying message via ajax you can use this javascript function to redirect with delay:

    // display message and ..
    setTimeout(function () {
       window.location.href = @Url.Action("Index","Home")
    }, 2000)
    

    It will redirect user after two seconds.Also you can take a look to this question: time delayed redirect?