0

So, when working with a Razor Class Library (RCL) in .NET Core 3.1 I'd like to have a predefined Index.cshtml file. And I would like it to use an AbstractIndexModel as its model, but this gives me errors.

basically i want to do something like this:

these two are in the RCL

public abstract class AbstractIndexModel : PageModel
{
    public IActionResult OnGet()
    {
        return Page();
    }

    public abstract string Greeting { get; }
}

The razor html

@page
@model AbstractIndexModel

@model.Greeting

And then on to the app, where we have an implementation of our abstract class:

public class IndexModel : AbstractIndexModel
{
    public override string Greeting => "Hello Stackoverflow!";
}

But this merely gives an error:

A suitable constructor for type 'Sample.Pages.AbstractIndexModel' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

I guess this happens because the model we set is not 'concrete' (its an abstract class) But I would suppose using the @inherit instead of @model would then do the trick. It does not. Any suggestions?

rasmus91
  • 3,024
  • 3
  • 20
  • 32

1 Answers1

2

you cannot use abstract class as @model. Also It needs to have public constructor. @inherit also does not work.

see difference between @model and @inherit

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • Damn it. Is there any way to achieve what I'm trying to do? (Or something similar)... I guess the only way I can really think of, that sort of fits, would be via dependency injection... Meaning IndexModel would just be a class, and take a configuration as a constrictor argument – rasmus91 Sep 24 '20 at 22:21