What is Invoke methods?
Below here is my viewcomponent()
method.
public IViewComponentResult Invoke()
{
ViewBag.SelectedCategory = RouteData?.Values["category"];
return
View(repository.Products.Select(x=>x.Category).Distinct().OrderBy(x=>x));
}
Is this is the anonymous function?
what is the name of this function ?
I tried to change "Invoke" with "func" but there is no syntax error but run-time error Occurred.
Is it Possible to have multiple methods with "Invoke" name and receiving different parameters In a single view component class.
I Know that view component use to render partial views for this we use helper which is this
@await Component.InvokeAsync("Here is the name of your view component")
"component.InvokeAsynk" Goes to your component class and finds The Invoke method and then follows this method
The Invoke
and InvokeAsync
methods on a ViewComponent
are the methods that define the logic of the view component. These are not anonymous functions. They are regular c# methods that simply have the names Invoke
and InvokeAsync
.
You don't need to define both of them in your view component. On the contrary you can only define one method.
Let us say you named it Invoke
and then you rename it to func
then you have changed the name of the method:
This is because the Asp.Net Core framework code that calls your view component expects it to have either a Invoke
or InvokeAsync
method and your ViewComponent
now has neither.
You can name your view component anything you like but the name of the method which gets called to invoke it must be called Invoke
or InvokeAsync
.
A ViewComponent
can only have one Invoke
or InvokeAsync
method. A ViewComponent
can't have both and it can't multiples of either one (with different parameters). Attempting to do so will produce the following runtime error:
View component 'ViewComponents.NameOfComponent' must have exactly one public method named 'InvokeAsync' or 'Invoke'.