ViewComponent taghelpers不渲染
有几个类似的问题,但似乎没有一个最近的问题,而微软的文档没有列出任何特殊步骤: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
2
字符串
参数作者
和标题
通过下面的taghelper传递,请勿匹配Invoke
的签名方法。Indoke
方法期望mybook
实例。TagHelper应像以下那样传递
mybook
对象。The 2
string
argumentsAuthor
andTitle
passed via the taghelper below, don't match the signature of theInvoke
method.That
Invoke
method expects aMyBook
instance.The taghelper should pass a
MyBook
object like below.