0

In my controller method I have the following simple comma separated string that I pass to my view using TempData:

var test = "hi,bye";
TempData["test"] = test;

In my view, I have the following js method:

var desks = @(TempData["test"]).split(",");

When I inspect the DOM using the Google Chrome developer console, I see the following:

var desks = hi,bye.split(",");

Why is the TempData string I pass losing it's quotes and becoming some sort of other object? What am I doing wrong?

2 Answers 2

3

The string you pass does not "lose" its quotes; the string value has no quotes, even though the string literal does.

You have to perform JavaScript string escaping yourself, something like this:

'@(TempData["test"].
    Replace("\\", "\\\\").
    Replace("\n", "\\n").
    Replace("\r", "\\r").
    Replace("'", "\\'"))'.split(",");

This will create an escaped string that is probably safe enough to output as a JavaScript string literal.

Another alternative is to call on JSON serialization to output the string, which will cover all your bases.

@Html.Raw((new JavaScriptSerializer()).Serialize(TempData["test"])) .split(",");

JavaScriptSerializer.Serialize will take any object (in this case, a string) and output it in a way that JavaScript understands.

Html.Raw will then make sure that the output from Serialize is written to the output stream without HTML-encoding.

The extra space between the last ) of C# code and the JavaScript .split is necessary. Without it, split is interpreted by Razor as the name of a C# method, which it is not.

To use the JavaScriptSerializer class, just add a @using System.Web.Script.Serialization directive at the top of your view.

Sign up to request clarification or add additional context in comments.

8 Comments

@((new JavaScriptSerializer()).Serialize(TempData["test"])).split(","); outputs: "hi,bye".split(","). Why is this happening?
Thanks but I think there's still a typo in there. Should be: @Html.Raw((new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(TempData["test"]).Split(','));
If you follow my advice to add a @using directive, you won't have to include all that.
I know. That's not where the typo was. There were one too many brackets in the wrong place and the Split required single quotes rather than double quotes.
Sorry Anders, didn't realize the space was important. It's working now. Learned something new today.
|
0

You can simply do it:

'@(TempData["test"])'.split(",");

Javascript won't interpret if it's a string in C# or not. It just write the string in the code. You need to manually input the quotes before and after the C# code, so javascript will read the hi,bye as string.

2 Comments

That won't work if the string value contains any of the following characters: ', \, linefeed characters.
Knowing that I will never have those characters in my specific context, this solution does work. But do note Anders' comment, this is not a 100% solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.