asp.net core rzaor page 中的ViewComponent 视图用法

浪淘沙2年前后端189

1、/Pages/Components 新建:

MyViewComponent.cs

using Microsoft.AspNetCore.Mvc;

namespace xxtsoft.Web.Entry.Pages.Components
{
    public class MyViewComponent: ViewComponent
    {
        public IViewComponentResult Invoke(string name)
        {
            // 这里可以处理一些逻辑,获取数据等
            var model = $"Hello, {name}";
            return View("Default", model); // 返回视图和数据
        }
    }
}

image.png

2、/Pages/Shared/Components/My/Default.cshtml

<div>
    <h2>@Model</h2>
</div>

image.png

3、页面引用

<!-- 调用 ViewComponent -->
@await Component.InvokeAsync("My", new { name = "xx hellow" })

image.png

相关文章

StringBuilder 简单写法

   public StringBuilder Sb { get; set; } = ne...

asp.net core 取得页面form中所有表单值

public IActionResult OnPostSubmit()         { &n...

asp.net core return Content 返回可解析的html

return Content(     "<!DOCTYPE html><html><body&g...

asp.net core razor .cshtml.cs 如何改变写入中文后变成ANSI格式为utf-8

根目录下,添加.editorconfig并写入[*.cshtml.cs] charset = utf-8已生成的文件另存为 utf-8格式...

取消 "将 null 文本或可能的 null 值转换为不可为 null 类型。 "提示

在 .csproj 文件中添加或修改:<PropertyGroup>  <Nullable>disable</Nullable></Pro...

asp.net core razor加载模板文件路径

  var templateContent = "${include(\"" + App.HostE...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。