Reputation: 123
I completely migrated a aspnet 5 rc1 project into the aspnet core-rc2 and made sure everything is working the same as before; however, I can't seem to get my ViewComponents to work in rc2 and I'm not exactly sure how to implement it in the "new" way.
Here is how I have it set up right now:
Somewhere in the a .cshtml file i have:
@Component.Invoke("AddActivity", Model.Id)
then I have the viewcomponent in a folder
public class AddActivityViewComponent : ViewComponent
{
public IViewComponentResult Invoke(int id)
{
ActivityViewModel model = new ActivityViewModel();
model.IssueId = id;
return View("ActivityModal", model);
}
}
and with this it would normally return the ActivityModal cshtml in the shared folder however RC2 seems to be asking for a class that implements IViewComponentHelper?
Please let me know if I'm doing something incorrectly or if there's a better way
Upvotes: 2
Views: 1619
Reputation: 56
What Isaac and Andrey said is corretct. I would just like to add: the name of the parameters need to match.
In your case, you need to call invoke with new { id = Model.Id}
as your parameter in the invoke method is called "id".
Upvotes: 0
Reputation: 45764
Note: The Sync APIs have been removed in rc2.
Your code may be like that:
public class AddActivityViewComponent : ViewComponent
{
public IViewComponentResult InvokeAsync(int id)
{
ActivityViewModel model = new ActivityViewModel();
model.IssueId = id;
return View("ActivityModal", model);
}
}
@Component.InvokeAsync<AddActivityViewComponent>(new { id = Model.Id})
or
@await Component.InvokeAsync<AddActivityViewComponent>(new { id = Model.Id})
Upvotes: 6