I’m trying to access string value from TempData from the script tag inside .cshtml
file.
<script>
var FromComTaskLibType = '@TempData["FromComTaskLibType"]';
console.log(FromComTaskLibType.toString())
</script>
Using this, I am getting value as AAAA
instead of getting like 'AAAA'
. I have called them with .toString()
. Still it is not working.
In controller, I am assigning this value like this:
public ActionResult LoginFromCOM(string libType)
{
TempData["FromComTaskLibType"] = libType;
//...
}
Here value of libType
is coming as "AAAA"
2
Answers
You must use the
ViewBag
at .cshtml file parameterExample:
Well, you don’t need to
convert to string
asTempData
already astring
. So you could try following way.Controller:
Script:
Output:
If You Want This ‘AAAA’ Output:
However, if you want your output as
'AAA'
withsingle qoute
You have to parse the string intoJSON.stringify
then has to replace the double qoute intosingle qoute
as following:Output: