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

浪淘沙11个月前后端69

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

相关文章

asp.net core弹出对话框最佳实践

 public void OnGet()  {         ...

返回月份当天日期

  int daysInMonth = DateTime.DaysInMonth(_yf.Year, _yf.Month);...

SqlSugar 无实体更新

 #region 保存反馈回复  /// <summary>  /// 保存反馈回复  /// <...

asp.net core razor 输出html

@Html.Raw(Model.HtmlContent)...

JNTemplate 文章标签解析

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

asp.net core 引用ckeditor编辑器

网页<script src="~/ckeditor/ckeditor.js"></script> <script src=&qu...

发表评论    

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