javascript 如何在javascript中获取Tempdata?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17058413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the Tempdata in javascript?
提问by RJ Uy
I have this method in my Controller that saves the value in a Tempdata, as shown below.
我在我的控制器中有这个方法将值保存在一个临时数据中,如下所示。
public Boolean SaveSession(string id) {
TempData["CurrentTab"] = id;
return true;
}
Now in my javascript, I want to get the value in that TempData. But when I alerted the value I got this value. "[object HTMLSpanElement]"
现在在我的 javascript 中,我想获取该 TempData 中的值。但是当我提醒价值时,我得到了这个价值。“[对象 HTMLSpanElement]”
@{
if (TempData["CurrentTab"] != null){
@:alert("" + @TempData["CurrentTab"].ToString())
}
}
How can I get the string value of that Tempdata?
如何获取该 Tempdata 的字符串值?
Thanks
谢谢
回答by haim770
The problem is that you're wrapping your TempData
value incorrectly.
问题是您TempData
错误地包装了您的价值。
Assuming your id
is my_span
, the JavaScript output is:
假设你id
是my_span
,JavaScript 输出是:
alert("" + my_span)
When you probably want:
当您可能想要:
alert("my_span")
The reason you see [object HTMLSpanElement]
is because the Browser tries to translate my_span
into document.getElementById('my_span')
(since it doesn't know of any other my_span
) and you actually have such (span
) element with that id.
您看到的原因[object HTMLSpanElement]
是因为浏览器试图转换my_span
为document.getElementById('my_span')
(因为它不知道任何其他my_span
),而您实际上拥有span
具有该 ID 的此类 ( ) 元素。
Try:
尝试:
@{
if (TempData["CurrentTab"] != null){
@:alert('@(TempData["CurrentTab"])');
}
}