asp中出错.NET核心:部分组件没有'不起作用



InvalidOperationException:名为'../..的视图组件/找不到共享/组件"。视图组件必须是公共的非抽象类,不包含任何泛型参数,并且必须用"ViewComponentAttribute"修饰,或者类名以"ViewComponent"后缀结尾。视图组件不得使用"NonViewComponentAttribute"进行修饰。

屏幕截图

@model ShopApp.WEBUI.ViewModels.ProductViewModel
@{
var popularClass = Model.Products.Count > 2 ? "popular" : "";
var products = Model.Products;
var categories = Model.Categories;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
body {
margin: 0;
padding: 0;
}
main {
display: inline;
}
.popular {
color: orangered;
}
</style>
</head>
<body>
@await Html.PartialAsync(@"../../shared/_navbar")
@await Html.PartialAsync(@"../../shared/_header");
<main>
<div class="container">
<div class="row">
<div class="col-md-4">
@await Component.InvokeAsync(@"../../Shared/Component",categories)
</div>
<div class="col-md-8">
@if (products.Count == 0)
{
@await Html.PartialAsync(@"../../shared/_NoProduc");
}
else
{
<div class="row">
@foreach (var product in products)
{
<div class="col-md-2">
@await Html.PartialAsync(@"../../shared/_product", product)
</div>
}
</div>
}
</div>
</div>
</div>
</main>
</body>
</html>

将组件文件夹的名称更改为Components,然后在其中创建具有Categories名称的文件夹,然后将Default.cshtml放入其中。

像这样调用

@await Component.InvokeAsync("Categories", new { place your parameters here })

类别ViewComponent必须来自ViewComponent

必须在CategoriesViewComponent类的InvokeAsynk方法中声明参数,并将其与要调用该组件的名称一起使用:new{parameter1="value"}

最新更新