通常情况下,使用TempData需要记住key的名称,本篇体验:通过帮助类,实现对TempData的设置、获取、删除。

关于传递信息的类:

namespace MvcApplication1.Models
{
public class Notification
{
public bool IsShow { get; set; }
public string Content { get; set; }
}
}

帮助类,使用TempData对信息处理:

using System;
using System.Web.Mvc;
using MvcApplication1.Models; namespace MvcApplication1.Extension
{
public class NotificationHelper
{
public static void SetNotification(Controller controller, bool isShow, string content)
{
Notification notification = new Notification()
{
IsShow = isShow,
Content = content
};
controller.TempData["n"] = notification;
} public static Notification GetNotification(Controller controller)
{
try
{
Notification notification = controller.TempData["n"] as Notification;
return notification;
}
catch (Exception ex)
{
//TODO:记录日志
return null;
}
} public static void RemoveNotification(Controller controller)
{
controller.TempData.Remove("n");
}
}
}

在HomeController中,把信息放到TempData中:

using System.Web.Mvc;
using MvcApplication1.Extension; namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{ public ActionResult Index()
{
NotificationHelper.SetNotification(this, true, "放假了~~");
return View();
} }
}

Home/Index.cshtml视图为:

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
} <h2>Index</h2> @Html.ActionLink("看看TestController那边是否放假了","Index","Test")

在TestController中,获取TempData存储的信息:

using System.Web.Mvc;
using MvcApplication1.Extension;
using MvcApplication1.Models; namespace MvcApplication1.Controllers
{
public class TestController : Controller
{ public ActionResult Index()
{
Notification notification = NotificationHelper.GetNotification(this);
if (notification != null)
{
ViewData["t"] = notification.IsShow;
ViewData["c"] = notification.Content;
}
return View();
}
}
}

Test/Index.cshtml视图:

@using MvcApplication1.Models
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
} <h2>Test</h2> @if (ViewData["t"] != null)
{
Notification notification = new Notification()
{
IsShow = (bool)ViewData["t"],
Content = ViewData["c"] as string
}; if (notification.IsShow)
{
@notification.Content
}
else
{
@: 有通知,但未发布
}
}
else
{
@: 暂无通知
}

MVC使用TempData跨控制器传递信息而无需记住key的名称的更多相关文章

  1. .Net Core----关于MVC中TempData持久化问题

    最近在做mvc跨控制器传值的时候发现一个问题,就是有时候TempData的值为null,然后查阅了许多资料,发现了许多都是逻辑和原理什么的(想看原理可以查看原理的文章,本文是用法),但是真正解决的办法 ...

  2. ASP.NET MVC的TempData(转载)

    本文章基于ASP.NET MVC Preview5. ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个控制器Action间传递临时的数据或者给View传递一些临时的数据,相信 ...

  3. ASP.NET MVC 使用TempData

    ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个控制器Action间传递临时的数据或者给View传递一些临时的数据,相信大家都看过"在ASP.NET页面间传值的方法 ...

  4. ThinkPHP跨控制器调用方法

    跨控制器调用方法 1. 先造对象,再调用里面的方法 $sc=new \Home\Controller\IndexController();  用绝对路径找echo $sc->ShuChu(); ...

  5. thinkphp3.2跨控制器调用其他模块的方法

    thinphp中前台后台都有互相调用方法,这样可以省去重复内容. 1 2 $hello = new \Admin\Common\Fun\hello(); $hello->hehe(); 调用其他 ...

  6. ThinPHP命名空间,连接数据库是要修改的配置文件,Model数据模型层,跨控制器调用,如何获取系统常量信息,

    一.命名空间(主要是为了实现自动加载类) *命名空间(相当于虚拟的目录),为了让类有一个统一的文件夹来管理(可以自动加载'类'),每个文件都要有命名空间*tp如何做命名空间:*TP框架下有一个初始命名 ...

  7. ASP.NET MVC 实现AJAX跨域请求方法《1》

    ASP.NET MVC 实现AJAX跨域请求的两种方法 通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据 ...

  8. ThinkPHP中的跨控制器调用与框架执行流程

    一.跨控制器调用 UserController.class.php <?php namespace Home/Controller use Think/Controller class User ...

  9. thinphp讲解(三)——空操作、空控制器、跨控制器、命名空间

    一.“空操作”本质意思:一个对象(控制器)调用本身不存在的操作方法 一般网站处于安全考虑不给用户提示任何错误信息 在tp里面控制器controller.class.php里有个_call()方法 所以 ...

随机推荐

  1. IntelliJ IDEA + Tomcat 部署问题

    首先要了解下 tomcat的 几种部署方式(大致分为静态部署和动态部署),可以百度,博客:http://qsfwy.iteye.com/blog/466461 IntelliJ IDEA 下部署项目的 ...

  2. java 学习网站

    http://how2j.cn/  教学网站 慕课视频下载网站 http://www.feemic.cn/mooc //慕课搜索和下载的网站http://www.soshoulu.com/tools/ ...

  3. Centos之目录处理命令

    linux中 关于目录 有几个重要概念 一个是 / 根目录  还有一个当前用户的家目录 比如 root用户的家目录是 /root  普通用户的家目录是/home/xxx 下 root登录 默认家目录 ...

  4. Spark(一)Spark简介

    一.官网介绍 1 什么是Spark 官网地址:http://spark.apache.org/ Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎.Spark是UC Berkel ...

  5. cetos6配置用msmtp和mutt发邮件(阿里云)

    Linux下可以直接用mail命令发送邮件,但是发件人是user@servername,如果机器没有外网的dns,其他人就无法回复.此时,有一个可以使用网络免费邮箱服务的邮件发送程序就比较重要了.ms ...

  6. C#实现盛大盛付通充值卡状态查询

    今天有这样一需求,要求能够查询盛付通卡的状态,官网如下 http://www.801335.com/status/index.htm 刚一打开网址,发现两个输入框加一个验证码,心中一喜不是小  cas ...

  7. Codeforces Round #334 (Div. 1) B. Moodular Arithmetic

    B - Moodular Arithmetic 题目大意:题意:告诉你p和k,其中(0<=k<=p-1),x属于{0,1,2,3,....,p-1},f函数要满足f(k*x%p)=k*f( ...

  8. Server sent passive reply with unroutable address. Using server address instead

    最近在linux服务器安装vsftp服务.经过一轮设置,终于可以连接上了,用winSCP连接,刷新目录就提示这个错误. 解决办法: vim /etc/vsftpd.conf  ,编辑配置文件,最后加上 ...

  9. linux查找文件或目录命令

    inux查找文件或目录命令,前提:知道文件或者目录的具体名字,例如:sphinx.conf find 查找  find / -name dirname  查找目录 find -name filenam ...

  10. python面向对象中类对象、实例对象、类变量、实例变量、类方法、实例方法、静态方法

    1. 类对象和实例对象 Python中一切皆对象,Python类本身也是一种对象,类定义完成后,会在当前作用域中定义一个以类名为名字的命名空间.类对象具有以下两种操作: 可以通过“类名()”的方式实例 ...