剃刀页面-动态切换CSS背景/主题

时间:2018-10-01 04:00:00

标签: html asp.net-core-2.1 razor-pages

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

即:

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

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

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

使用的VS模板: C#> .Net Core> .Net Core Web应用程序>剃刀页面

1 个答案:

答案 0 :(得分:0)

MyPage.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";
        }
    }
}