rokdbest
rokdbest

Reputation: 39

How to add a PageModel to a Razor component?

I am looking through some code and someone made a Razor component with a separate PageModel.

How can I create something like this?

Razor component with its PageModel:

In folder:

I don't understand how to make a Razor component in two parts like this. Can someone explain how to do this?

Upvotes: 0

Views: 1708

Answers (1)

rokdbest
rokdbest

Reputation: 39

  1. Create a Razor component
  2. Create the class item with the same name as the Razor component. The files will automatically nest (like Razor Pages). There is a button in Solution Explorer, which allows you to disable automatic nesting.
  3. Rename the .cs (class item) to public partial class
  4. You can delete the @{} from the component

There is also a second method in which the HTML page inherits the class. Here is a good YouTube video on that: https://www.youtube.com/watch?v=4CeEFuY_ieg

Upvotes: 0

Related Questions