通常情况下,使用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. SSIS 学习之旅 FTP文件传输-FTP任务

    这一章主要讲解一下FTP控件. 设计:   通过Demon库的Users表数据生成CSV文件.   生成后的CSV文件抛送到FTP指定目录下. 其他控件的使用这里就不做详细讲解了.大家如果有不懂得可以 ...

  2. InstallShield在MySQL和Oracle中执行SQL脚本的方法InstallShield在MySQL和Oracle中执行SQL脚本的方法

    简述 InstallShield已经内建了对MySQL和Oracle的支持.但是这个功能是通过ODBC实现的,它对SQL脚本的格式要求非常严格,因此已经通过官方客户端测试的脚本在IS中执行时往往就会报 ...

  3. hmm学习笔记(二)

    学习hmm近一周的时间,做个总结. 参考  李航的<统计学习方法>第9章,第10章 本文包含: 1.hmm模型 2.前向后向算法 3.Baum-Welch算法 4.维特比算法 1.hmm模 ...

  4. JSON解析代码

    /** * 解析有数据头的纯数组 */ private void parseHaveHeaderJArray() { //拿到本地JSON 并转成String String strByJson = J ...

  5. 面试题46:求1+2+ …… +n

    题目:求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 通常求1+2+…+n除了用公式n(n+1)/2之外,无外 ...

  6. Django: ImportError: No module named 'corsheaders'

    pip3 install django-cors-headers #设置PIP镜像 pip3 --trusted-host pypi.python.org install django-cors-he ...

  7. SpringSecurity3基础篇

    Spring Security 是一种基于Spring AOP 和Servlet过滤器的安全框架,它提供了全面的安全性解决方案,同时在Web请求级和方法调用级处理身份确认和授权.在Spring Fra ...

  8. The file will have its original line endings in your working directory.

    在空仓库的情况下,add,出现一下问题 The file will have its original line endings in your working directory. 当报这个警告时是 ...

  9. OpenGL笔记<5> shader 调试信息获取 Debug

    我们今天来讲调试信息,这个东西讲起来会比较无聊,因为都是一些函数调用,没啥可讲的,函数就是那样用的,不过其效果挺好玩的,同时在程序设计中也是很必要的,所以还是来写一下,不过,就是因为知识比较固定且简单 ...

  10. mysql store procedure 存储过程

    参考资料: 1.http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html 2.https://dev.mysql.com/doc/refman/5.7/ ...