Reputation: 46322
I am trying to assign HTML to TempData as such:
TempData["FilesUploaded"] = "<option value= '" + file.FileName + "'>" + file.FileName + "</option>";
I need to get this value in Jquery. As such, I do the following:
var val = '@TempData["FilesUploaded"]';
alert(val);
What I am finding is that the characters are coming as such:
<option value= 'PS Report #36178.pdf'>PS Report #36178.pdf</option>
I tried to do it enclose TempData in @Html.Raw(... but that did not work as well.
Here is what I tried:
var val = '@Html.Raw((string)TempData["FilesUploaded"])';
alert(val);
What is odd is that it doesn't work in that the alert doesn't even come up.
Upvotes: 2
Views: 11159
Reputation: 40576
Use HttpUtility.JavaScriptStringEncode
to encode the string as JavaScript:
var val = '@Html.Raw(HttpUtility.JavaScriptStringEncode((string)TempData["FilesUploaded"]))';
Upvotes: 11