biprism avatar image
0 Votes"
biprism asked baluraju-6888 commented

[InjectAttribute] in VIew Model class

I injected the NavigationManager in my view Blazor Razor component and is available to use it. I'm calling from View a method in VIewModel that needs to use NavigationManager. I used [Inject] attribute to initialize the NavigationManager public property, but it is not getting injected.

One approach is to pass NaviagtionManger to VIewModel or set the navigationmanager property in VIewModel from View.
Is there a different better way to do it?

a.Razor Injects the NaviagtionManagaer using @Inject and is available within the view.
aViewModel.cs tries to Inject the NaviagtionManager and is not doing it and is not available.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @biprism, Could you pls share your view model and how do you use this view model? For injecting the service, there are two common ways. One way is to use [Inject] attribute, the other way is to inject by constructor.

0 Votes 0 ·

private IUserAuthorization UserAuthorization { get; set; } = default!;

THis is how I inject my object in the ViewModel (SignatureViewModel) for a Razor component. I know it is good when I inject by constructor.

In Program.cs, I have the following
//Register the Authorization Objects
builder.Services.AddSingleton<IUserAuthorization, UserAuthorization>();

builder.Services.AddScoped<ISignatureViewModel, SignatureViewModel>()

0 Votes 0 ·

0 Answers