码迷,mamicode.com
首页 > Web开发 > 详细

如何为asp.net mvc的action上统一加viewbag

时间:2020-02-05 09:35:30      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:nbsp   增强   attribute   ilo   inf   null   code   属性   ISE   

1、在项目中存在一些情况,需要为action加一些viewbag属性增强可维护性。

[TitleFooterAttribute]
        // GET: yewulingyu
        public ActionResult Index()
        {
            int toal = 0;
            List<GongSiYeWu> list = new BLL_GongSiYewu().Search(-1, -1, out toal, null);
            return View(list);
        }

然后我们看 TitleFooterAttribute的定义

  /// <summary>
    /// 为ViewBag添加默认参数
    /// </summary>
    public class TitleFooterAttribute : FilterAttribute, IResultFilter
    {
        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
        }

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            BLL_GongsiInfo bll = new BLL_GongsiInfo();
            GongsiInfo info = bll.GetSingle(Guid.Parse(ConfigurationManager.AppSettings["GongsiID"]));
            filterContext.Controller.ViewBag.GongsiFooter = info.GongsiFooter;
            filterContext.Controller.ViewBag.GongsiName = info.GongsiName;
            filterContext.Controller.ViewBag.GongsiSearchDes = info.GongsiSearchDes;
            filterContext.Controller.ViewBag.GongsiSearchkey = info.GongsiSearchkey;
            filterContext.Controller.ViewBag.GongsiCultrue = info.GongsiCultrue;

            filterContext.Controller.ViewBag.GongsiShortName = info.GongsiShortName;
            filterContext.Controller.ViewBag.GongsiLogo = info.GongsiLogo;
        }
    }

 

如何为asp.net mvc的action上统一加viewbag

标签:nbsp   增强   attribute   ilo   inf   null   code   属性   ISE   

原文地址:https://www.cnblogs.com/sexintercourse/p/12262232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!