@model for _layout.cshtml on MVC4?

后端 未结 4 1289
眼角桃花
眼角桃花 2021-01-02 09:42

I was wondering if there\'s a way to specify a model for the _layout.cshtml file, i\'ve seen lots of posts with the basic same question with people replying with \"alternati

4条回答
  •  隐瞒了意图╮
    2021-01-02 10:00

    In BaseController you can declare any model as property.

    public class BaseController : Controller
    {
        public BaseController ()
        {
            MyTag = new TagModel ();  // or get db, take any value from there           
        }
    
        public TagModel MyTag { get; set; }
    }
    

    In action:

    ViewBag.MyTag = MyTag ;
    

    And in _Layout.cshtml, you can use

    @{
      var myTag = (TagModel)ViewBag.MyTag;
    }
    

提交回复
热议问题