zoukankan      html  css  js  c++  java
  • 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")

    1


    在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
    {
      @: 暂无通知
    }

    2

  • 相关阅读:
    牛客网2017年校招全国统一模拟笔试(第三场)编程题集合
    侯捷STL学习(六)--深入list && Iterator traits
    侯捷STL学习(五)--allocator和容器之间的实现关系
    侯捷STL学习(四)--OOP-GP/操作符重载-泛化特化
    侯捷STL学习(三)--分配器测试
    侯捷STL学习(二)--序列容器测试
    牛客网2017年校招全国统一模拟笔试(第一场)编程题集合
    数据库面试题(一)
    SQL基础理论题
    基本SQL练习题--选课经典例题
  • 原文地址:https://www.cnblogs.com/darrenji/p/3764290.html
Copyright © 2011-2022 走看看