I've been following the excellent BlazorTrain series by Carl Franklin and after watching Episodes 24 and 25 I've attempted adopting the MVVM approach in my current project.
The issue I've hit is on pages which have a parameter as in
@page "/ViewTask/{Id}"
Prior to conversion to MVVM my code on ViewTask was similar to
[Inject] protected NavigationManager NavigationManager { get; set; }
[Inject] protected IMyService MyService{ get; set; }
[Inject] protected IMyService2 MyService2{ get; set; }
[Inject] protected IMyService3 MyService3{ get; set; }
[Parameter] public string Id { get; set; }
In my new ViewModel, I am injecting the services in the constructor as below however the parameter Id is null. Is there a straightforward way to pass parameters to the ViewModel class? I'm thinking of making the the method GetWorkTask() public and accept a parameter of Id and pass the parameter that way but wanted to see if there are better ways?
public class ViewTaskViewModel : IViewTaskViewModel
{
public ViewTaskViewModel(NavigationManager navigationManager,
IMyService myService,
IMyService2 myService2,
IMyService3 myService3)
{
NavigationManager = navigationManager;
MyService = myService;
MyService2 = myService;
MyService3 = myService3
InitializeViewModel().GetAwaiter().GetResult();
}
[Parameter] public string Id { get; set; }
private NavigationManager NavigationManager { get; set; }
private IMyService MyService { get; set; }
private IMyService2 MyService2 { get; set; }
private IMyService3 MyService3 { get; set; }
protected async Task InitializeViewModel()
{
...
GetWorkTask()
}
private void GetWorkTask()
{
int taskId = int.Parse(Id);
...
}
}