Razor 视图页面中布局文件引用-Razor动态布局文件
2024-12-04

布局文件引用,如果不想引用默认的
 

@{
    Layout = "_IndexLayout";//新建的布局文件
}

动态布局文件引用:_

indexLayout.cshtml

@{

    var menu = Model.Menu;  // 引用传递的 MenuModel
}

Menu.cshtml文件

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data;

namespace Web.Pages.Shared
{
    public class MenuModel : PageModel
    {
        //基础数据
        public string Logo { get; set; } = "";
        public string ManageTitle { get; set; } = "";
        public string Phone { get; set; } = "";
        public string Email { get; set; } = "";
        public string Address { get; set; } = "";
        public string CopyRight { get; set; } = "";
        public string ManageUrl { get; set; } = "";
        //list菜单
        public List<ArticleCategory.ArticleCategoryModel> ArticleCategoryList = new List<ArticleCategory.ArticleCategoryModel>();
        

        public void GetMenu()
        {
            //页面基础参数赋值
            Logo =Tools.ConfigurationHelper.GetConfigValue("ManageSet:Logo");
            ManageTitle = Tools.ConfigurationHelper.GetConfigValue("ManageSet:ManageTitle");
            Phone = Tools.ConfigurationHelper.GetConfigValue("ManageSet:Phone");
            Email = Tools.ConfigurationHelper.GetConfigValue("ManageSet:Email");
            Address = Tools.ConfigurationHelper.GetConfigValue("ManageSet:Address");
            CopyRight = Tools.ConfigurationHelper.GetConfigValue("ManageSet:CopyRight");
            ManageUrl = Tools.ConfigurationHelper.GetConfigValue("ManageSet:ManageUrl");
            //菜单信息
            ArticleCategory.ArticleCategoryDal Dal = new ArticleCategory.ArticleCategoryDal();

            DataTable Datas = Dal.Get(1);
            ArticleCategoryList = Tools.DataTableToList.DatatableToList<ArticleCategory.ArticleCategoryModel>(Datas);
        }
    }


}

Index.cshtml首页文件 引入导航条


        //传递参数布局页面
        public Web.Pages.Shared.MenuModel? Menu { get; set; }
        public void OnGet()
        {
            //传递参数布局页面
            var menu = new Web.Pages.Shared.MenuModel();
            menu.GetMenu();
            Menu = menu;  // 传递 Menu 数据
        }