How to get tempdata in javascript?

I have this method in my controller that stores the value in 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 this TempData. But when I warned about the meaning, I got that value. "[object HTMLSpanElement]"

@{
         if (TempData["CurrentTab"] != null){           
            @:alert("" + @TempData["CurrentTab"].ToString())                
        }
    }

How can I get the string value of this tempdata?

thank

+1
source share
1 answer

The problem is that you are not correctly porting the value TempData.

Assuming yours idis equal my_span, JavaScript output:

alert("" + my_span)

When you probably want to:

alert("my_span")

, [object HTMLSpanElement], , my_span document.getElementById('my_span') ( my_span), (span) id.

Try:

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

All Articles