标签:
今天,遍历一个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;
}
}
}
Uncaught ReferenceError: is not defined
标签:
原文地址:http://www.cnblogs.com/liuyuanhao/p/4483567.html