MVC使用TempData跨控制器傳遞資訊而無需記住key的名稱

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

通常情況下,使用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{  @: 暫無通知}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.