简体   繁体   中英

jQuery alert message box redirect upon click ok

I am using ASP.NET MVC 4, so how do I redirect the message box upon click ok back to index page?

Here's my code

<script type="text/javascript">
$(function errMsgBox() {
    alert("Please select the Correct Activity and Task");           
    @Html.ActionLink("Back to List", "/Index") 
});
</script> 

You don't need to use razor here anymore. Just use the window.location object.

<script type="text/javascript">
    $(function errMsgBox() {
        alert("Please select the Correct Activity and Task");
        window.location = "/Home/Index";
    });
</script>

It's better to use such construction:

<script type="text/javascript">
    $(function errMsgBox() {
        alert("Please select the Correct Activity and Task");           
        window.location = '@Url.Action("Index", "Home")';
    });

Because in that case if you change your routes then it will use new route instead of specifying full route manually in Html. It's better to handle routes in one place, and it's not so good the hardcode them in Html, as in that case if route changes, then you need to update all hardcoded places.

And another point is parameters. If your action has parameter, it's also better to use Url.Action, as it will use parameters based on your route rules without hardcoding them too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM