-1

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 :)

tr4cks
  • 11
  • 4

2 Answers2

0

The example you mentioned above is a much simpler and neater solution. From an architectural point of view, it is scalable and maintainable. This is the correct way of doing this. It has also been described simply. I don't know what is your problem with that.

BTW if you are using .NET Core you can also consider using Dependency Injection in your layout:

@inject UserManager<ApplicationUser> UserManager
Majid Shahabfar
  • 693
  • 1
  • 11
  • 19
  • Indeed I find the example perfect in my situation the problem is that it is given for .net core MVC (webapp MVC) and I do not use MVC but directly razor pages (webapp). – tr4cks Apr 11 '21 at 21:59
  • No. It is for Razor. you can easily apply it to your project. – Majid Shahabfar Apr 11 '21 at 22:03
  • Yes for razor MVC with controllers but in webapp without MVC you don't have any controller like in the example. Microsoft has implemented 2 types of webapp. The function it overloads is not the same in my case. In their case they inherit from Controller and in my case from PageModel. – tr4cks Apr 11 '21 at 22:10
  • It doesn't make any sense. You can apply it for your PageModel – Majid Shahabfar Apr 11 '21 at 22:19
  • So I would like an example because it is not the same implementation. I can't even call the View() function. Do you use razor mvc or razor without mvc? – tr4cks Apr 11 '21 at 22:25
  • I use razor and if I need web api in same project I use mvc too – Majid Shahabfar Apr 11 '21 at 22:28
  • Well I can't return a view from the Page() method because it's not a controller. If you know how to do it from a PageModel I would like you to tell me how to do it. Thank you :) – tr4cks Apr 11 '21 at 22:33
  • https://www.learnrazorpages.com/razor-pages/action-results – Majid Shahabfar Apr 11 '21 at 22:45
  • Yes, I know, what I am trying to do is to adapt the example given above to these methods which are not the same at all. At no time does it return a view on the page you gave me as in the example: ```csharp public ActionResult Page() { return View(new LayoutModel(new Customer() { Name = "Test" }, "Title"); } ``` – tr4cks Apr 11 '21 at 22:52
  • You can't assign a model to a PageResult because the model is not assignable. ```csharp public object Model { get; } in class PageResult ``` – tr4cks Apr 11 '21 at 23:01
  • You can define your model with [BindProperty] attribute in your page model and access it in your razor view by @model YourModel directive. – Majid Shahabfar Apr 12 '21 at 09:01
0

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

tr4cks
  • 11
  • 4