欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用ASP.Net?Core中的Razor實(shí)現(xiàn)動(dòng)態(tài)菜單

 更新時(shí)間:2022年04月28日 15:45:37   作者:微風(fēng)吹過~  
這篇文章主要介紹了如何利用ASP.Net?Core中的Razor實(shí)現(xiàn)動(dòng)態(tài)菜單功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

準(zhǔn)備

1.框架

.netcore  版本 yishaadmin開源框架

2.模板 

本文模板使用adminlte3.0,文檔地址

3.菜單表關(guān)鍵字段 

id 表主鍵(當(dāng)前菜單)

ParentId 父級(jí)ID(父級(jí)菜單 為0時(shí)為頂級(jí)菜單,也可能為內(nèi)容)

MenuUrl 菜單地址(只有頁面有地址,本身菜單是空)

MenuType 菜單類型(1是菜單 2是頁面 3是按鈕)

MenuIcon 圖標(biāo)樣式

4.菜單表實(shí)體

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Util;

namespace YiSha.Entity.SystemManage
{
    [Table("SysMenu")]
    public class MenuEntity : BaseExtensionEntity
    {
        [JsonConverter(typeof(StringJsonConverter))]
        public long? ParentId { get; set; }

        public string MenuName { get; set; }

        public string MenuIcon { get; set; }

        public string MenuUrl { get; set; }

        public string MenuTarget { get; set; }

        public int? MenuSort { get; set; }

        public int? MenuType { get; set; }

        public int? MenuStatus { get; set; }
        public string Authorize { get; set; }

        public string Remark { get; set; }

        [NotMapped]
        public string ParentName { get; set; }
    }
}

本文是由于框架內(nèi)置菜單不支持頂級(jí)菜單顯示為內(nèi)容,以及菜單最多只支持三級(jí)菜單的問題,故進(jìn)行了調(diào)整。

1.實(shí)現(xiàn)思路

下圖1區(qū)域渲染為菜單,菜單通過點(diǎn)擊URL將內(nèi)容填充到2區(qū)域。  

2.編碼

2.1  建立渲染內(nèi)容填充方法

將傳進(jìn)來的url通過ajax調(diào)用最終渲染到內(nèi)容區(qū)域(id為#Content的Div中),其中beforeSend方法顯示Loadding 可根據(jù)需要自行調(diào)整。url為{area:exists}/{controller=Home}/{action=Index}以及{controller=Home}/{action=Index}根據(jù)框架配置填寫至菜單

function LoadContent(url) {
        if (url == null || url == "")
            return;

        $.ajax({
            url: url,
            beforeSend: function (XHR) {
                $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' 
                 + "加載中..." + '</div>', css: { border: "none", backgroundColor: 'transparent' } });
            },
            success: function (data) {
                $("#Content").html(data);
                setTimeout(function () { $.unblockUI(); }, 100);
            },
            error: function (data, status, e) {
                $("#Content").html("頁面加載失敗," + data.status + "," + url + "<br />" + data.responseText);
                setTimeout(function () { $.unblockUI(); }, 100);
            }
        });
  }

2.2  建立分部視圖

通過建立分部視圖MenuTree,循環(huán)傳入的菜單,初始化時(shí)先獲取父級(jí)ID(ParentId)為0并且類別(MenuType)不為按鈕的菜單集合進(jìn)行循環(huán),根據(jù)menuEntity.MenuUrl判斷是否為頁面,如果依然為菜單則使用Html.PartialAsync("MenuTree")調(diào)用自身來實(shí)現(xiàn)遞歸,第二次則根據(jù)ViewData["Menu"]傳入的當(dāng)前id作為父級(jí)id來尋找子集,直到尋找到最后的層級(jí)。

@using System.Collections.Generic
@using YiSha.Entity.SystemManage;
@model List<MenuEntity>


@{
    if (Model.Any())
    {
        long id = 0L;
        var menu = ViewData["Menu"] as MenuEntity;
        if (menu != null)
          id = menu.Id.Value;

        @foreach (var menuEntity in Model.Where(o => o.ParentId == id && o.MenuType != (int)MenuTypeEnum.Button))
        {
            var icno = string.IsNullOrEmpty(menuEntity.MenuIcon) ? "fa fa-comment" : menuEntity.MenuIcon;
            @if (!string.IsNullOrEmpty(menuEntity.MenuUrl))
            {
               <li class="nav-item">
                   <a href="#" rel="external nofollow"  rel="external nofollow"  class="nav-link" onclick="LoadContent('@menuEntity.MenuUrl')">
                       <i class="nav-icon @icno"></i>
                       <p>
                           @menuEntity.MenuName
                       </p>
                   </a>
               </li>
            }
            else
            {
                ViewData["Menu"] = menuEntity;
                <li class="nav-item">
                    <a href="#" rel="external nofollow"  rel="external nofollow"  class="nav-link">
                    <i class="nav-icon @icno"></i>
                    <p>
                        @menuEntity.MenuName
                        <i class="fas fa-angle-left right"></i>
                    </p>
                    </a>
                    <ul class="nav nav-treeview">
                         @await Html.PartialAsync("MenuTree",
                         Model,new ViewDataDictionary(ViewData))
                    </ul>
               </li>
            }
         }
    }
}

2.3 調(diào)用分布視圖

<aside class="main-sidebar sidebar-dark-primary elevation-4" style="width:200px;position:fixed">
            <!-- Brand Logo -->
            
            <!-- Sidebar -->
            <div class="sidebar">

                <!-- Sidebar Menu -->
                <nav class="mt-2">
                    <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
                        <!-- Add icons to the links using the .nav-icon class
    with font-awesome or any other icon font library -->

                        <li class="nav-header" style="font-size:1.0rem">
                            <img src="~/yisha/img/logo1.png" style="width: 30px; height: 30px; " />
                            任務(wù)管理系統(tǒng)
                        </li>

                        @await Html.PartialAsync("MenuTree", Model)
                    </ul>
                </nav>
                <!-- /.sidebar-menu -->
            </div>
            <!-- /.sidebar -->
        </aside>

        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper" id="mainhead">
            <div id="Content">

            </div>
        </div>

到此這篇關(guān)于利用ASP.Net Core中的Razor實(shí)現(xiàn)動(dòng)態(tài)菜單的文章就介紹到這了,更多相關(guān)ASP.Net Core Razor動(dòng)態(tài)菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能示例【附demo源碼】

    ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能示例【附demo源碼】

    這篇文章主要介紹了ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net生成驗(yàn)證碼的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2017-07-07
  • ASP.NET Core處理錯(cuò)誤環(huán)境

    ASP.NET Core處理錯(cuò)誤環(huán)境

    這篇文章介紹了ASP.NET Core處理錯(cuò)誤環(huán)境的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • .Net Core日志記錄之日志配置

    .Net Core日志記錄之日志配置

    這篇文章介紹了.Net Core日志記錄之日志配置,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • ASP.NET MVC生成靜態(tài)頁面的方法

    ASP.NET MVC生成靜態(tài)頁面的方法

    對(duì)于網(wǎng)站來說,生成純html靜態(tài)頁面除了有利于seo外,還可以減輕網(wǎng)站的負(fù)載能力和提高網(wǎng)站性能。本篇文章主要介紹了ASP.NET MVC生成靜態(tài)頁面的方法,有興趣的可以了解一下。
    2017-03-03
  • asp.net中WebResponse 跨域訪問實(shí)例代碼

    asp.net中WebResponse 跨域訪問實(shí)例代碼

    一篇朋友很久前寫的asp.net中WebResponse 跨域訪問示例,下面我轉(zhuǎn)過來與大家一起學(xué)習(xí)學(xué)習(xí),希望文章對(duì)大家會(huì)有幫助
    2014-01-01
  • 一個(gè)簡單的ASP.NET驗(yàn)證碼

    一個(gè)簡單的ASP.NET驗(yàn)證碼

    這篇文章主要為大家詳細(xì)介紹了一個(gè)簡單的ASP.NET驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • .NET?Core中使用gRPC的方法

    .NET?Core中使用gRPC的方法

    gRPC 一開始由 google 開發(fā),是一款語言中立、平臺(tái)中立、開源的遠(yuǎn)程過程調(diào)用(RPC)系統(tǒng),所以叫g(shù)(google)RPC,這篇文章主要介紹了.NET?Core中使用gRPC,需要的朋友可以參考下
    2022-09-09
  • asp.net漢字轉(zhuǎn)拼音和獲取漢字首字母的代碼

    asp.net漢字轉(zhuǎn)拼音和獲取漢字首字母的代碼

    在網(wǎng)上找到的好東西。以后asp.net下漢字轉(zhuǎn)成拼音就方便多了
    2008-07-07
  • 一次.net?core異步線程設(shè)置超時(shí)時(shí)間的實(shí)戰(zhàn)記錄

    一次.net?core異步線程設(shè)置超時(shí)時(shí)間的實(shí)戰(zhàn)記錄

    這篇文章主要給大家介紹了關(guān)于.net?core異步線程設(shè)置超時(shí)時(shí)間的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 讀取XML并綁定至RadioButtonList實(shí)現(xiàn)思路及演示動(dòng)畫

    讀取XML并綁定至RadioButtonList實(shí)現(xiàn)思路及演示動(dòng)畫

    讀取XML的文檔,可以使用System.Data.DataSet類別中的ReadXml()方法,在aspx網(wǎng)頁上拉一個(gè)RadioButtonList控件,用來顯示XML的數(shù)據(jù),接下來,用DataSet去讀取剛才寫好的獲取XML文件的屬性,即可完成
    2013-01-01

最新評(píng)論