如何获得的TempData在JavaScript? [英] How to get the Tempdata in javascript?

查看:193
本文介绍了如何获得的TempData在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的控制器这种方法,在TempData的保存价值,如下图所示。

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

现在在我的JavaScript,我想在这TempData的值。但是,当我惊动了价值我得到这个值。 [对象HTMLSpanElement]

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())                
        }
    }

我怎样才能得到的TempData的字符串值?

How can I get the string value of that Tempdata?

感谢

推荐答案

的问题是,你是不正确的包装你的的TempData 值。

The problem is that you're wrapping your TempData value incorrectly.

假设你的 ID my_span ,JavaScript的输出是:

Assuming your id is my_span, the JavaScript output is:

alert("" + my_span)

当你可能想:

alert("my_span")

您看到的原因 [对象HTMLSpanElement] 是因为浏览器尝试翻译 my_span 的document.getElementById('my_span')(因为它不知道任何其他的 my_span ),你居然有这样的(跨度)元素与ID。

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.

尝试:

@{
     if (TempData["CurrentTab"] != null){
        @:alert('@(TempData["CurrentTab"])');
    }
}

这篇关于如何获得的TempData在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆