ViewComponent taghelpers不渲染

发布于 2025-01-25 04:43:42 字数 2142 浏览 2 评论 0原文

有几个类似的问题,但似乎没有一个最近的问题,而微软的文档没有列出任何特殊步骤: https://learn.microsoft.com/en-us/aspnet/aspnet/core/mvc/mvc/mvc/mvc/views/view-components/view-components?view = = = appnetcore -6.0

@await component.invokeasync(“ topbooks” ...无问题,但< vc:top-books/> dik我不添加@AddtagHelper *,WebApplication1 index.cshtml 。

​ Studio 2022

public class TopBooksViewComponent : ViewComponent {
    public IViewComponentResult Invoke(MyBook book) {
        return View(book);
    }
}

Visual 组件/topbooksviewcomponent.cs

public class MyBook {
    public string Author { get; set; }

    public string Title { get; set; }
}

保存为models/mybook.cs

接下来,我创建了视图:

@model WebApplication1.Models.MyBook

<h1>Top Books view component</h1>

<p>@Model.Title by @Model.Author</p>

已保存为pages/sharone/components/topbooks/default.cshtml

然后,为了尝试一下,我打开pages/index.cshtml并将其修改为:

@page
@using WebApplication1.Components
@using WebApplication1.Models
@model IndexModel
@addTagHelper *, WebApplication1

@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    @await Component.InvokeAsync("TopBooks", new MyBook() {Author = "John Smith", Title = "How to do stuff!"})
    
    <vc:top-books Author="Shepard" Title="The Reapers are coming!"></vc:top-books>
</div>

正如我所说,@await component.invokeasync毫无问题,但没有问题,但是Taghelper什么都不做。据我所知,不需要其他任何特殊设置,它归结为@AddtaGhelper *,WebApplication1不起作用。但是,我不确定还能将其更改为什么。 WebApplication1也是我项目的名称。

There are a few similar questions, but none of which seem to be recent, and Microsoft's documentation doesn't list any special steps: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-6.0

The @await Component.InvokeAsync("TopBooks"... works without issue, but the <vc:top-books /> does not render. I added @addTagHelper *, WebApplication1 to the top of Index.cshtml but the TagHelper doesn't work. If you inspect the source it's just written out as is.

I also tried adding @addTagHelper *, WebApplication1 to _ViewImports.cshtml instead with no difference in behaviour.

I created a basic ASP.NET Core Razor Pages .NET 6 application in Visual Studio 2022. File > New > ASP.NET Core Web App > Next until completed

I then created a very simple ViewComponent and model:

public class TopBooksViewComponent : ViewComponent {
    public IViewComponentResult Invoke(MyBook book) {
        return View(book);
    }
}

saved as Components/TopBooksViewComponent.cs

public class MyBook {
    public string Author { get; set; }

    public string Title { get; set; }
}

saved as Models/MyBook.cs

Next, I created the View:

@model WebApplication1.Models.MyBook

<h1>Top Books view component</h1>

<p>@Model.Title by @Model.Author</p>

saved as Pages/Shared/Components/TopBooks/Default.cshtml

Then, to try it out I opened Pages/Index.cshtml and modified it to this:

@page
@using WebApplication1.Components
@using WebApplication1.Models
@model IndexModel
@addTagHelper *, WebApplication1

@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    @await Component.InvokeAsync("TopBooks", new MyBook() {Author = "John Smith", Title = "How to do stuff!"})
    
    <vc:top-books Author="Shepard" Title="The Reapers are coming!"></vc:top-books>
</div>

As I said, the @await Component.InvokeAsync works with no problem, but the TagHelper does nothing. As far as I can tell there isn't any other special setup required and it comes down to @addTagHelper *, WebApplication1 not working. However, I am not sure what else I can change it to. WebApplication1 is the name of my project as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2025-02-01 04:43:42

2 字符串参数作者标题通过下面的taghelper传递,请勿匹配Invoke的签名方法。

<vc:top-books Author="Shepard" Title="The Reapers are coming!"></vc:top-books>

Indoke方法期望mybook实例。

public IViewComponentResult Invoke(MyBook book)

TagHelper应像以下那样传递mybook对象。

<vc:top-books book="@(new MyBook { Author = "Shepard", Title = "The Reapers are coming!" })"></vc:top-books>

The 2 string arguments Author and Title passed via the taghelper below, don't match the signature of the Invoke method.

<vc:top-books Author="Shepard" Title="The Reapers are coming!"></vc:top-books>

That Invoke method expects a MyBook instance.

public IViewComponentResult Invoke(MyBook book)

The taghelper should pass a MyBook object like below.

<vc:top-books book="@(new MyBook { Author = "Shepard", Title = "The Reapers are coming!" })"></vc:top-books>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文