Vencovsky
Vencovsky

Reputation: 31683

How to add a Partial Class to a component in Blazor in Visual Studio 2019?

I'm not finding where should I click to create a partial class in visual studio 2019.

After I create a new "Razor Component" file, I can't add the partial class to it.

Where do I add it?

Upvotes: 10

Views: 17226

Answers (3)

sergiokml
sergiokml

Reputation: 121

Another way: (vs 2022)

Right click the @Code section, pick Quick Actions and Refactorings, select Extract block to code behind.

enter image description here

Upvotes: 5

Alexan
Alexan

Reputation: 8637

There are two ways to add code behind to Razor component:

  1. Add Base class and in component add @inherits this class, how is described in this answer. In this case base class shouldn't be partial.

  2. Since October 2019 we can use partial classes. You can just add class name with the same name, adding .cs extension, mark it as partial class:

enter image description here

And you don't need to use @inherits in this case.

See Partial class support doc.

Upvotes: 18

yob
yob

Reputation: 528

Something like this? : - razor page with "code-behind" class

enter image description here

Upvotes: 3

Related Questions