Search code examples
c#asp.net-core.net-corerazor

Passing model to _Layout.cshtml (webapp version)


I would like to be able to pass a model to my base layout _layout.cshtml exactly like in this example. The problem is that this example is only valid for the MVC framework and I use razor pages without MVC.

I saw that we could override the Page() method in a class inheriting from PageModel but I have the impression that it is not possible to provide the model at view creation time.

public class IndexModel : PageModel {
    public override PageResult Page() {
        return base.Page();  // how to pass a custom model?
    }
}

If anyone has any ideas or another way to do it I'm a taker. Thanks :)


Solution

  • I don't know how I didn't think of it before. It's a simple inheritance with PageModel and your bound attributes is available in your _layout and in your final page. No need to create the view or to use a generic as with the MVC framework.

    Here it is the very simple solution :)

    Thanks to those who spent time searching