gpt4 book ai didi

c# - 在派生的 RazorPage 中获取 View Component 的渲染结果

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:31 25 4
gpt4 key购买 nike

我需要从派生的 RazorPage 中获取 View 组件的渲染结果。在 .cshtml 文件中,我可以做

@await Component.InvokeAsync("MyButton", new MyModel())

现在,如果我有我的 razor 页面继承的这个类:

public class MyRazorPage<TModel> : RazorPage<TModel>
{
public async Task<HtmlString> GetViewComponentForExample()
{
// I cannot access the injected "Component" this way...
string temp = await Component.InvokeAsync("MyButton", new MyModel()).Result.WriteTo();
return new HtmlString(temp);
}
}

在上面的方法 GetViewComponentForExample() 中,我无法使用注入(inject)的 Component 属性。我怎样才能访问 Component 属性,或者我怎样才能完成上述任务?

最佳答案

你能做的就是这个(ASP .Net Core 2.0)

如果你有这样的组件:

[ViewComponent(Name = "Cart")]
public class CartViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
return View("");
}
}

你可以像这样在 Controller 中调用它

public class OtherController : Controller
{
[HttpGet]
public IActionResult Cart()
{
return ViewComponent("Cart");
}
}

这将返回一个呈现的组件

关于c# - 在派生的 RazorPage 中获取 View Component 的渲染结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43666601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com