繁体   English   中英

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

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

InvalidOperationException: 找不到名为“../../Shared/Component”的视图组件。 视图组件必须是公共非抽象类,不包含任何泛型参数,并且要么使用“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 ,然后在其中创建具有类别名称的文件夹,然后将Default.cshtml放入其中。

像这样打电话

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

CategoriesViewComponent 必须从ViewComponent 继承

您的参数必须在 CategoriesViewComponent 类的 InvokeAsynk 方法中声明,并在要调用此组件的位置使用它的名称: new { parameter1 = "value" }

暂无
暂无

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

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