Parthasus
Parthasus

Reputation: 123

asp.net core 1 rc2: Creating a view component

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

Answers (3)

markusvt
markusvt

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

enet
enet

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

Hypnobrew
Hypnobrew

Reputation: 1140

It seems like they changed how Components are invoked in RC2. Now the Invoke method only takes one argument with "key/value pairs" as parameters.
So maybe something like this would work:

@Component.Invoke("AddActivity", new { id = Model.Id})

More info here.

Upvotes: 0

Related Questions