How to get the Tempdata in javascript?

筅森魡賤 提交于 2019-12-24 10:46:46

问题


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


回答1:


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

Assuming your id is my_span, the JavaScript output is:

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.

Try:

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


来源:https://stackoverflow.com/questions/17058413/how-to-get-the-tempdata-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!