zoukankan      html  css  js  c++  java
  • Uncaught ReferenceError: is not defined

    今天,遍历一个HashSet集合对象,想用链接绑定集合对象的值,通过POST方式提交到控制器。结果程序无反应,按F12键进入调试模式,谷歌总是提示Uncaught ReferenceError:   is not defined这个错误。

    原来是虽然是传递的值,但是在函数传参的时候也要加引号,加上引号后就不会提示 Uncaught ReferenceError:   is not define 了。

    View :

    @using MajorConstruction.Helpers;

    @{
    ViewBag.Title = "更改主题";
    }

    <h2>@ViewBag.Title</h2>
    <hr />
    <div class="container">
    <div class="row">
    @foreach (var theme in Bootstrap.Themes)
    {
    <div class="col-md-3">
    <div class="panel panel-default">
    <div class="panel-body">
    <h2>@theme</h2>
    <br />
    <br />
    <p>
    @{
    bool IsCurrent = theme == HttpContext.Current.Application["CssTheme"].ToString();   //Application 应用程序全局对象。
    string btnDisabled = IsCurrent ? "disabled" : null;
    }
    <a class="btn btn-success @btnDisabled" href="javascript:SetActive('@theme')">应用主题</a> <!--把主题绑定在JavaScript函数上,通过调用Javascript函数提交隐藏表彰。-->
    <!--还需要特别注意,调用 javascript 函数的参数@theme必须加引号,如果不加引号,总是谷哥浏览器总是提示 Uncaught ReferenceError: XX 未定义 is not defined,因为@theme变量本来就不是值类型,而是HashSet类型-->
    </p>
    </div>

    </div>
    </div>
    }

    </div>
    </div>


    @using (Html.BeginForm("ChangeTheme", "Theme", FormMethod.Post, new { id = "themeForm" }))
    {
    @Html.AntiForgeryToken()
    <input type="hidden" name="theme" id="themeInput" />
    }

    @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
    var changeThemeUrl = '@Url.Action("ChangeTheme")';


    function SetActive(themename) {
    $("#themeInput").val(themename); //将参数值传递给隐藏表单。

    $("#themeForm").submit(); //提交表单,将 theme=themename 发送至控制器POST 方法中。

    }

    </script>

    }

    控制器:

    public class ThemeController : Controller
    {
    // GET: Admin/Theme
    public ActionResult ChangeTheme()
    {

    return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ChangeTheme(string theme)
    {
    HttpContext.Application["CssTheme"] = theme;
    return View();
    }

    Bootstrap 类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace MajorConstruction.Helpers
    {
    public class Bootstrap
    {
    public const string BundleBase = "~/Content/css";

    //类作为一个类的成员。
    public class Theme
    {
    public const string Cerulean = "Cerulean";
    public const string Darkly = "Darkly";
    public const string Flatly = "Flatly";
    public const string Spacelab = "Spacelab";
    public const string Stock = "Stock";
    public const string Superhero = "Superhero";
    }

    public static HashSet<string> Themes = new HashSet<string>
    {
    Theme.Cerulean,
    Theme.Darkly,
    Theme.Flatly,
    Theme.Spacelab,
    Theme.Stock,
    Theme.Superhero
    };


    public static string Bundle(string themename)
    {
    return BundleBase + themename;
    }
    }
    }

  • 相关阅读:
    解决:npm中 下载速度慢 和(无法将“nrm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确, 然后再试一次)。
    maven(一) maven到底是个啥玩意~
    Luogu3959 NOIP2017宝藏(状压dp)
    Luogu3953 NOIP2017逛公园(最短路+拓扑排序+动态规划)
    Luogu3952 NOIP2017时间复杂度
    BZOJ4753 JSOI2016最佳团体(分数规划+树形dp)
    BZOJ1975 SDOI2010魔法猪学院(启发式搜索+最短路+堆)
    BZOJ4105 THUSC2015平方运算(线段树)
    BZOJ5109 CodePlus 2017大吉大利,晚上吃鸡!(最短路+拓扑排序+bitset)
    Luogu3731 HAOI2017新型城市化(二分图匹配+强连通分量)
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4483567.html
Copyright © 2011-2022 走看看