gpt4 book ai didi

c# - 找不到名为 'PreviewCV' 的 View 组件

转载 作者:行者123 更新时间:2023-12-04 15:47:05 24 4
gpt4 key购买 nike

我已经创建了一个 View 组件。

public class PreviewCVComponent : ViewComponent
{
..


public async Task<IViewComponentResult> InvokeAsync(int id)
{
return View();
}
}

我在 views/shared/components 中添加了一个名为 PreviewCV 的文件夹。在该文件夹下,我添加了一个名为 Default.cshtml 的 View 。

从另一个 View 调用该组件。它位于 views/CV 下,名称为 CV.cshtml。

我正在尝试使用

调用组件
@await Component.InvokeAsync("PreviewCV", new { id = -1 })

这导致:

InvalidOperationException: A view component named 'PreviewCV" could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. A view component must not be decorated with 'NonViewComponentAttribute'.

我正在使用 .net 核心。

最佳答案

--在模型文件夹中

文件名:TestViewComponent.cs

--在TestViewComponent类中

[ViewComponent(Name = "TestViewComponent")] //Solution
public class TestViewComponent: ViewComponent
{
public IViewComponentResult Invoke()
{
return View();
}
}

ViewComponent放在后缀中解决问题。

关于c# - 找不到名为 'PreviewCV' 的 View 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55250565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com