简体   繁体   English

asp .NET 核心中的错误:部分组件不起作用

[英]Error in asp .NET core: Partial component doesn't work

InvalidOperationException: A view component named '../../Shared/Component' could not be found. InvalidOperationException: 找不到名为“../../Shared/Component”的视图组件。 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.视图组件必须是公共非抽象类,不包含任何泛型参数,并且要么使用“ViewComponentAttribute”修饰,要么具有以“ViewComponent”后缀结尾的类名。 A view component must not be decorated with 'NonViewComponentAttribute'.视图组件不得使用“NonViewComponentAttribute”进行修饰。

Screenshot截屏

@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>

Change name of component folder to Components and then create folder in that with Categories name and then put Default.cshtml in it. 将组件文件夹的名称更改为Components ,然后在其中创建具有类别名称的文件夹,然后将Default.cshtml放入其中。

call like this像这样打电话

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

CategoriesViewComponent must inheited from ViewComponent CategoriesViewComponent 必须从ViewComponent 继承

your parameters must be declared in the InvokeAsynk method of CategoriesViewComponent class and use it with its name where you want to Invoke this component: new { parameter1 = "value" }您的参数必须在 CategoriesViewComponent 类的 InvokeAsynk 方法中声明,并在要调用此组件的位置使用它的名称: new { parameter1 = "value" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM