@model for _layout.cshtml on MVC4?

后端 未结 4 1320
眼角桃花
眼角桃花 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

    You can add BaseModel to _Layout.

    @model BaseModel
    

    Then all models inherit from that BaseModel class.

    public class MyModel : BaseModel
    {
    }
    

    As others stated, it is not a good practice. If your model forgets to inherit from BaseModel, it'll throws exception at run time. However, it is up to you.

提交回复
热议问题