.net core using ViewComponent

.net core component is a fragmentary view in ViewComponent, common reuse may be used for establishment of functional components

First, create a new class DemoViewComponent ( must end ViewComponent ) and inheritance ViewComponent

using Microsoft.AspNetCore.Mvc;
using NetCoreApiDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NetCorePortal.Components
{
    public class DemoViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync()
        {
            List<tbl_page> pageList = new List<tbl_page>();
            for (int= I 0 ; I < 10 ; I ++ ) 
            { 
                pageList.Add ( new new tbl_page () 
                { 
                    page_no = i.ToString (), 
                    page_name = i.ToString () 
                }); 
            } 
            return View (pageList); // where no Back ViewName corresponding view file is Default.cshtml
             // return view ( "D", pageList); // here is returned ViewName "D" corresponding to the view file is D.cshtml 
        } 
    } 
}

 

Second, the establishment Components directory under View / Share directory, and to establish the corresponding Default.cshtml Demo directories and files in this directory  

@model IEnumerable<NetCoreApiDemo.Model.tbl_page>
<h1>Demo IViewComponentResult</h1>
<table>
    <tr>
        <th>page_no</th>
        <th>page_name</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.page_no</td>
            <td>@item.page_name</td>
        </tr>
    }
</table>

 

Guess you like

Origin www.cnblogs.com/zbspace/p/11620131.html