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()方法 所以 ...
随机推荐
- SpringCloud Config Bus webhook 只能刷新config server 不能刷新config client
在 https://github.com/spring-cloud/spring-cloud-bus/issues/124 中有提到 版本 SpringCloud:Greenwich.RC1 原因 由 ...
- Linux学习笔记:ls和ll命令
list显示当前目录中的文件名字,不加参数时显示除隐藏文件外的所有文件及目录的名字. ll 等同于 ls -l-r 对目录反向排序(按字母)-t 以时间排序-u 以文件上次被访问的时间排序-x 按列输 ...
- jboss各种测试方式归类
不跨工程访问(如:HBase) 跨工程访问(如:Business) 不部署到服务器上 部署到服务器上 不部署到服务器上 部署到服务器上 Junit测试 实例化直接调用 true true Fals ...
- Mongodb配置:error:10061 由于目标计算机积极拒绝,无法连接
相信很多学Node的同学,在进入MongoDB后台管理 Shell的时候都会“遇到error:10061 由于目标计算机积极拒绝,无法连接”这种情况,很多情况都是dbpath与dblog的路径没有配置 ...
- Hive(八)Hive的Shell操作与压缩存储
一.Hive的命令行 1.Hive支持的一些命令 Command Description quit Use quit or exit to leave the interactive shell. s ...
- linux的IPC进程通信方式-匿名管道(一)
linux的IPC进程通信-匿名管道 什么是管道 如果你使用过Linux的命令,那么对于管道这个名词你一定不会感觉到陌生,因为我们通常通过符号"|"来使用管道,但是管道的真正定义是 ...
- Web Api 的 路由机制
ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web API 是一种用于在 .NET Framework 上构 ...
- Mongoose关于当天日期的查询
参考:https://blog.csdn.net/difffate/article/details/70312894 Ant Design Pro中,有关于日期的查询条件,但日期是以数字表示的 Req ...
- Python网络编程之socket应用
1 引言 本篇主要对Python下网络编程中用到的socket模块进行初步总结.首先从网络基础理论出发,介绍了TCP协议和UDP协议:然后总结了socket中的常用函数:最后通过实际代码展示基本函数的 ...
- 在centOS使用systemctl配置启动多个tomcat
公司服务器使用的是阿里云CentOS7,CentOS7和CentOS6目前最大区别就是service变成了现在的systemctl,简单的查了一下并结合使用,发现systemctl功能上等同于6上面的 ...