【发布时间】:2013-09-03 07:01:34
【问题描述】:
我正在尝试使用以下代码在 MVC 视图中获取 web.config 值。
function GetMinMax(Code) {
var newCode= Code;
var minCode =newCode+"MinValue";
var maxCode =newCode+"MaxValue";
var minValue = @Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings[minCode]);
var maxValue = @Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings[maxCode]);
return [minValue, maxValue];
}
但是,javscript 变量 minCode 和 maxCode 是未定义的。请告诉我是否有可能实现。
【问题讨论】:
-
我真的很惊讶为什么我对这段代码投了反对票。当我的以下代码在 document.ready 下完美运行时???
var minRange = @Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PriceMinRange"]); var maxRange = @Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxRange"]); var minVal = @Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MinSelectedValue"]); var maxVal = @Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxSelectedValue"]); -
我没有否决这个问题,但我猜这样做的人是因为您不了解在服务器和客户端上执行的内容之间的区别。 Javascript 与服务器端代码。恕我直言,这不是投反对票的理由。
-
你明白了。我知道剃刀允许我们编写嵌入在 JS 或 HTML 视图中的服务器端代码。如果我硬编码
AppSettings["MaxSelectedValue"]。它在这里也很完美。我只是希望它是动态的:(
标签: c# javascript asp.net-mvc razor