MVC改变默认主题

  • 视图:
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Show</title>
    @*
        传输路径:@Url.Action("传输路径的方法")
        指明样式类型:type="text/css"
    *@
    <link href="@Url.Action("/Css")" rel="stylesheet" type="text/css" />
</head>
<body>
    <div>
        <h1>用Show方法改变字体颜色:</h1>
        <h3>获取到的值为:@ViewBag.color</h3>
        <form action="/Default/Show" method="post">
            <input type="radio" name="color" value="black" />黑色<br />
            <input type="radio" name="color" value="red" />红色<br />
            <input type="radio" name="color" value="blue" />蓝色<br />
            <input type="submit" value="保存" />
        </form>
    </div>
</body>
  • 控制器:
public ActionResult Show()
        {
            return View();
        }
        [HttpPost] //接收post的值才执行该方法
        public ActionResult Show(string color)
        {
            //用cookie存储该值【第一次初始化时,存储该new HttpCookie("color", color),第二次后,直接取cookie中的值来用】
            HttpCookie cookie = Request.Cookies["color"] ?? new HttpCookie("color", color);
            cookie.Expires = DateTime.MaxValue;//永不过期
            ViewBag.color = color;
            cookie.Value = color;
            //缓存cookie
            Response.SetCookie(cookie);
            return View();
        }

        public ActionResult Css()
        {
            HttpCookie cookie = Request.Cookies["color"] ?? new HttpCookie("color", "black");
            switch (cookie.Value)
            {
                case "red":
                    return Content("body{ color:red }", "text/css");
                    break;
                case "blue":
                    return Content("body{ color:blue }", "text/css");
                    break;
                default://前面都不执行,就执行它
                    return Content("body{ color:black }", "text/css");
                    break;
            }
            //return View();
        }
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值