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

浪淘沙2年前后端174

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

相关文章

JNTemplate 文章标签解析

using JinianNet.JNTemplate; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetC...

设置edge浏览器自动填充 input password值

<form autocomplete="off">    <input type="password" name=&quo...

ubuntu 删除asp.net core 8.0

1. 查找已安装的 ASP.NET Core 8.0 版本dotnet --list-runtimes dotnet --list-sdks2. 删除 ASP.NET Core...

asp.net core furion 前端传入json,后端api接入并角析

{   "result": {     "cover": {  ...

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

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

asp.net core sqlsugar timestamp 防sql注入方法

 string sql= "insert into ts (title,dx,zt,tsnr,tpe,jhsj,bz,guid)&...

发表评论    

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