Miguel Moura
Miguel Moura

Reputation: 39484

Sync Invoke Component in ASP.NET MVC Core 1.0

I am trying to Invoke a Sync component using ASP.NET Core 1.0 so I have:

public class ExampleViewComponent : ViewComponent {

  public IViewComponentResult InvokeAsync() {
    ExampleModel model = new ExampleModel(2);
    return View(model);
  }

}

And trying to invoke it as follows:

@Component.InvokeAsync("Example")

But I then get the error:

InvalidOperationException: Method 'InvokeAsync' of view component 'MVCApp.Components.ExampleViewComponent' should be declared to return Task<T>.    

I am using InvokeAsync because Invoke was removed in 1.0.

How should I do this?

Upvotes: 0

Views: 2773

Answers (1)

Tseng
Tseng

Reputation: 64259

InvalidOperationException: Method 'InvokeAsync' of view component 'MVCApp.Components.ExampleViewComponent' should be declared to return Task<T>.

Read the message and follow the instructions...

public class ExampleViewComponent : ViewComponent {
    public Task<IViewComponentResult> InvokeAsync() {
        ExampleModel model = new ExampleModel(2);

        // returns a finished task
        return Task.FromResult<IViewComponentResult>(View(model));
    }
}

Upvotes: 4

Related Questions