どのようにasp.Netmvcのactionにviewbagを統一する

3095 ワード

1、プロジェクトにはいくつかの状況があり、actionにviewbag属性を加えてメンテナンス性を強化する必要がある.
[TitleFooterAttribute]
        // GET: yewulingyu
        public ActionResult Index()
        {
            int toal = 0;
            List list = new BLL_GongSiYewu().Search(-1, -1, out toal, null);
            return View(list);
        }

そしてTitleFooterAttributeの定義を見てみましょう /// /// ViewBag /// 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; } }