2017-06-13 100 views
0

我正在使用mvc核心2.0项目,但查看组件不工作。Mvc Core 2.0查看组件

我得到这个错误

InvalidOperationException异常:找不到一个 '调用' 或 'InvokeAsync' 法视分量

[ViewComponent(Name = "Footer")] 
public class FooterViewComponent : ViewComponent 
{ 
    public ViewViewComponentResult Invoke() 
    {  
     var model = new FooterModel(); 

     return View(model); 
    } 
} 

回答

1

更改调用的返回类型IViewComponentResult

[ViewComponent(Name = "Footer")] 
public class FooterViewComponent : ViewComponent 
{ 
    public IViewComponentResult Invoke() 
    { 
     var model = new FooterModel(); 

     return View(model); 
    } 
} 

并在视图中使用它像:@await Component.InvokeAsync(typeof(FooterViewComponent))

我使用ASP.Net核心2.0和视图组件工作正常。

0

在我的实现中,视图组件的Invoke方法必须是一个Async方法,并且应该返回一个Task作为输出。这是如何工作对我来说:

我的视图组件:

public class XTestEntityViewComponent : ViewComponent 
{ 
    public async Task<IViewComponentResult> InvokeAsync(int id) 
    { 
     // Put your stuff here . . . 
    } 
} 

而且你可以从任何观点在应用程序中访问如下:

@await Component.InvokeAsync("XTestEntity", new { id = 4 }) 

我希望这有助于