首页 > 解决方案 > Razor-pages - 动态切换 css 背景/主题

问题描述

问题:如何动态更改 div 类/主题。从“nul”到“bg-info”再到“bg-success”

IE:

<div class="container body-content">
<div class="container body-content bg-info">
<div class="container body-content bg-success">

(Razor-pages 2.1.1 / Visual Studio 2017 / EF 2.1.1)(引导程序 - 无控制器结构)

设置:EF/localDB 切换或列出选项。

使用的 VS 模板:C# > .Net Core > .Net Core Web 应用程序 > Razor-pages

标签: htmlasp.net-core-2.1razor-pages

解决方案


我的页面.cshtml:

@page
@using MyProject.Pages
@model MyPageModel

<div class="container body-content@Model.Theme">

MyPage.cshtml.cs:

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

namespace MyProject.Pages
{
    public class MyPageModel : PageModel
    {
        public string Theme { get; set; } = "";

        public void OnGet()
        {
            if(showInfo) Theme = "bg-info";
            else if(showInfo) Theme = "bg-success";
        }
    }
}

推荐阅读