asp .net core 视图组件

一、创建

1、视图组件类

a、新建文件夹 ViewComponents

b、新建类 xxViewComponent.cs

注意:继承 ViewComponent

c、创建方法 

using Microsoft.AspNetCore.Mvc;

namespace PartialViewDemon.ViewComponents
{
    public class TableViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync() 
        {
            return View();
        }
    }
}

2、视图

a、新建文件夹 Shared/Components/xx

b、新建视图

默认视图 Default.cshtml,其它视图名需要 视图组件类 return View() 指定

二、使用

1、引用(普通视图)

@await Component.InvokeAsync("xx")  // 前面命名的视图组件名称

2、ViewData/ViewBag

 注意:视图组件类 传递给 视图,ViewData和ViewBag和控制器的ViewData和ViewBag不同

a、视图组件类

namespace PartialViewDemon.ViewComponents
{
    // 视图组件
    public class TableViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync() 
        {
            ViewBag.Title = "员工";
            ViewBag.Persons = new List<Person> 
            { 
                new Person { Name = "张三", Job = "后端" },
                new Person { Name = "李四", Job = "前端" }
            };
            return View("Table");
        }
    }
}

b、视图

<table class="table table-striped">
    <h5>@ViewBag.Title</h5>
    <thead>
        <tr>
            <th>姓名</th>
            <th>工作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (Person person in ViewBag.Persons)
        {
            <tr>
                <td>@person.Name</td>
                <td>@person.Job</td>
            </tr>
        }
        
        
    </tbody>
</table>

3、model

a、视图组件类

public class TableViewComponent : ViewComponent
{
    public async Task<IViewComponentResult> InvokeAsync() 
    {
        PersonModel personModel = new PersonModel 
        {
            Title = "员工",
            Persons = new List<Person>
            {
                new Person { Name = "张三", Job = "后端" },
                new Person { Name = "李四", Job = "前端" }
            }
        };
        return View("Table", personModel);
    }
}

b、视图

@model PersonModel
<table class="table table-striped">
    <h5>@Model.Title</h5>
    <thead>
        <tr>
            <th>姓名</th>
            <th>工作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (Person person in Model.Persons ?? [])
        {
            <tr>
                <td>@person.Name</td>
                <td>@person.Job</td>
            </tr>
        }
        
    </tbody>
</table>

4、带参数的视图组件

a、参数传递

普通视图 => 视图组件类 => 视图(视图组件) 

注意:参数要相同(视图传递的参数名称 和 视图组件类 接收的参数名一致)

b、视图

@{
    PersonModel personModel = new PersonModel
    {
        Title = "员工",
        Persons = new List<Person>
        {
            new Person { Name = "张三", Job = "后端" },
            new Person { Name = "李四", Job = "前端" }
        }
    };
}
@await Component.InvokeAsync("Table", new { personObj  = personModel })

c、视图组件类

namespace PartialViewDemon.ViewComponents
{
    // 视图组件
    public class TableViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync(PersonModel personObj) 
        {
            return View("Table", personObj);
        }
    }
}

d、视图(视图组件视图)

@model PersonModel
<table class="table table-striped">
    <h5>@Model.Title</h5>
    <thead>
        <tr>
            <th>姓名</th>
            <th>工作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (Person person in Model.Persons ?? [])
        {
            <tr>
                <td>@person.Name</td>
                <td>@person.Job</td>
            </tr>
        }
        
    </tbody>
</table>

 

posted @ 2026-11-25 17:40  市丸银  阅读(5)  评论(0)    收藏  举报