Nate Pet
Nate Pet

Reputation: 46322

MVC TempData passing to Jquery

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:

    &lt;option value= &#39;PS Report #36178.pdf&#39;&gt;PS Report #36178.pdf&lt;/option&gt;

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

Answers (1)

Cristian Lupascu
Cristian Lupascu

Reputation: 40576

Use HttpUtility.JavaScriptStringEncode to encode the string as JavaScript:

var val = '@Html.Raw(HttpUtility.JavaScriptStringEncode((string)TempData["FilesUploaded"]))';

Upvotes: 11

Related Questions