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;
    }
    }
    }

  • 相关阅读:
    jpa @onetomany 级联查询时会有重复数据,去重问题
    jpa/hibernate @onetomany 使用left join 添加多条件,可以使用过滤器filters (with-clause not allowed on fetched associations; use filters异常信息)
    如何使用多数据源,同时使用jpa和jdbctemplate
    mysql中使用enum,如何获取所有可能的值
    jpa返回List<Map<String, Object>>相当于jdbctemplate的queryForlist
    git bash各种乱码问题,已解决
    Kafka学习整理五(Consumer配置)
    创建Kafka0.8.2生产者与消费者
    Kafka消费组(consumer group)
    Kafka学习之四 Kafka常用命令
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4483567.html
Copyright © 2011-2022 走看看