asp.net mvc跳转提示实现

在执行完操作后往往需要显示执行的结果,可以使用js来实现执行后跳转并显示信息的功能。

视图

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>跳转提示</title>
</head>
<body>
    <div class="system-message">
        <h1>@ViewBag.Redirect["message"]</h1>
        <p class="jump">
            页面自动 <a id="href" href="@ViewBag.Redirect["url"]">跳转</a> 等待时间: <b id="wait">@ViewBag.Redirect["time"]</b>
        </p>
    </div>
    <script type="text/javascript">
    (function () {
        var wait = document.getElementById('wait'), href = document.getElementById('href').href;
        var interval = setInterval(function () {
            var time = --wait.innerHTML;
            if (time <= 0) {
                location.href = href;
                clearInterval(interval);
            };
        }, 1000);
    })();
    </script>
</body>
</html>

跳转数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EditorOnline.Util
{
    public class RedirectData
    {
        public static Dictionary<String, Object> GetSuccess(String url, String message)
        {
            return GetRedirect(url, message, 2);
        }

        public static Dictionary<String, Object> GetRedirect(String url, String message, int time)
        {
            Dictionary<String, Object> dic = new Dictionary<string, object>();
            dic.Add("url", url);
            dic.Add("message", message);
            dic.Add("time", time);
            return dic;
        }

        public static Dictionary<String, Object> GetFail(String url, String message)
        {
            return GetRedirect(url, message, 3);
        }

    }
}

跳转功能

/// <summary>
        /// 页面跳转视图
        /// </summary>
        /// <param name="url"></param>
        /// <param name="message"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        protected ViewResult Redirect(String url, String message, int time)
        {
            this.ViewBag.Redirect = RedirectData.GetRedirect(url, message, time);
            return this.View("redirect");
        }
调用示例

return Redirect("/Home/Index", "执行成功", 2);
在需要跳转的控制器中使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值