简体   繁体   English

单击确定后,jQuery警报消息框重定向

[英]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? 我正在使用ASP.NET MVC 4,因此如何在单击“确定”回到索引页后重定向消息框?

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. 只需使用window.location对象。

<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. 因为在这种情况下,如果您更改路线,则它将使用新路线,而不是在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. 最好在一个地方处理路线,并且在Html中对它们进行硬编码也不是一件好事,因为在这种情况下,如果路线发生变化,则需要更新所有硬编码的地点。

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. 如果您的操作具有参数,则最好使用Url.Action,因为它将基于您的路由规则使用参数,而无需对其进行硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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