MVC使用TempData跨控制器传递信息而无需记住key的名称
通常情况下,使用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的名称的更多相关文章
- .Net Core----关于MVC中TempData持久化问题
最近在做mvc跨控制器传值的时候发现一个问题,就是有时候TempData的值为null,然后查阅了许多资料,发现了许多都是逻辑和原理什么的(想看原理可以查看原理的文章,本文是用法),但是真正解决的办法 ...
- ASP.NET MVC的TempData(转载)
本文章基于ASP.NET MVC Preview5. ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个控制器Action间传递临时的数据或者给View传递一些临时的数据,相信 ...
- ASP.NET MVC 使用TempData
ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个控制器Action间传递临时的数据或者给View传递一些临时的数据,相信大家都看过"在ASP.NET页面间传值的方法 ...
- ThinkPHP跨控制器调用方法
跨控制器调用方法 1. 先造对象,再调用里面的方法 $sc=new \Home\Controller\IndexController(); 用绝对路径找echo $sc->ShuChu(); ...
- thinkphp3.2跨控制器调用其他模块的方法
thinphp中前台后台都有互相调用方法,这样可以省去重复内容. 1 2 $hello = new \Admin\Common\Fun\hello(); $hello->hehe(); 调用其他 ...
- ThinPHP命名空间,连接数据库是要修改的配置文件,Model数据模型层,跨控制器调用,如何获取系统常量信息,
一.命名空间(主要是为了实现自动加载类) *命名空间(相当于虚拟的目录),为了让类有一个统一的文件夹来管理(可以自动加载'类'),每个文件都要有命名空间*tp如何做命名空间:*TP框架下有一个初始命名 ...
- ASP.NET MVC 实现AJAX跨域请求方法《1》
ASP.NET MVC 实现AJAX跨域请求的两种方法 通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据 ...
- ThinkPHP中的跨控制器调用与框架执行流程
一.跨控制器调用 UserController.class.php <?php namespace Home/Controller use Think/Controller class User ...
- thinphp讲解(三)——空操作、空控制器、跨控制器、命名空间
一.“空操作”本质意思:一个对象(控制器)调用本身不存在的操作方法 一般网站处于安全考虑不给用户提示任何错误信息 在tp里面控制器controller.class.php里有个_call()方法 所以 ...
随机推荐
- 【读书笔记】Android平台的漏洞挖掘和分析
最近比较关注移动端的安全,以后也打算向安卓平台的安全发展.这篇博文主要是记录一些研究Android安全的读书笔记. Fuzzing技术的核心是样本生成技术 测试Android平台的组件间通信功能使用的 ...
- rocketmq在linux上安装
1.下载bin压缩包,然后解压 官网下载地址:https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.3.2/rocketmq-all-4.3.2- ...
- spring boot 之使用mapstruct
最近在阅读swagger源码,当看到 springfox.documentation.swagger2.mappers.ModelMapper 类时,无意中看到该类上面使用的 org.mapstruc ...
- springboot之使用@ConfigurationProperties注解
springboot之使用@ConfigurationProperties注解 代码已经上传至github https://github.com/gittorlight/springboot-exam ...
- Mongoose关于当天日期的查询
参考:https://blog.csdn.net/difffate/article/details/70312894 Ant Design Pro中,有关于日期的查询条件,但日期是以数字表示的 Req ...
- C#使用Pechkin与CPechkin生成PDF
http://blog.sina.com.cn/s/blog_5a52cec70102wpcf.html 1. Pechkin 从NuGet程序管理器中获得Pechkin,代码示例如下: ...
- python 关键知识点
学习资源:笨方法学习 python3 将变量传递给脚本--argv 脚本:你编写的 .py 文件. argv 参数变量(argument variable)保存着你运行 python 脚本的参数. i ...
- poj-2777线段树刷题
title: poj-2777线段树刷题 date: 2018-10-16 20:01:07 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道线段树的染色问题,,, ...
- python流行的原因
12~14年是云计算最火的几年,大批创业公司和巨头挤破头地进军云计算领域,大家都在做IAAS,最著名的云计算开源平台OpenStack 就是基于Python 开发的,为此催生出不少Python 岗位 ...
- Android DecorView浅析
摘要 一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. ...