2

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

3 Answers 3

6

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})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ! Your suggestion with the above code + the @await Component.InvokeAsync(new { id = Model.Id}) worked perfectly.
This still did not work for me. I had to change the method signature like this "public async Task<IViewComponentResult> InvokeAsync(int id)" ...... and had to call it with await otherwise it would return a weird string instead of the view
0

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.

1 Comment

I saw this, but I'm even able to call a simple ViewComponent with no parameters in RC2. Don't know whats going on. I'll try to dig into.
0

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.