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]"
@{
if (TempData["CurrentTab"] != null){
@:alert("" + @TempData["CurrentTab"].ToString())
}
}
How can I get the string value of that Tempdata?
Thanks
The problem is that you're wrapping your
TempData
value incorrectly.Assuming your
id
ismy_span
, the JavaScript output is:When you probably want:
The reason you see
[object HTMLSpanElement]
is because the Browser tries to translatemy_span
intodocument.getElementById('my_span')
(since it doesn't know of any othermy_span
) and you actually have such (span
) element with that id.Try: