1

Could you please help me find what I am doing wrong?

My model has a string[] field called AllKnownColors. I am trying to use it in my javascript code like this:

    var clrs = JSON.parse('@Html.Raw(Model.AllKnownColors)');

But it doesn't work. The debugger shows me this:

var clrs = JSON.parse('System.String[]');

and the following error: Uncaught SyntaxError: Unexpected token S

And I cannot figure out what is wrong.

Thanks.

3
  • What would you have liked @Html.Raw(Model.AllKnownColors) to result in? As you can see, it turns into "System.String[]" when turned into a string (using ToString() internally as Html.Raw() does). Commented Jan 19, 2015 at 20:41
  • should tag this with asp related tags, issue isn't really javascript related Commented Jan 19, 2015 at 20:44
  • Could you please tell what would be the right way? Thanks. Commented Jan 19, 2015 at 20:45

2 Answers 2

1

The code

var clrs = JSON.parse('@Html.Raw(Model.AllKnownColors)');

is similar to

var clrs = JSON.parse('@Html.Raw(Model.AllKnownColors.ToString())');

You need to convert Model.AllKnownColors to json. You can use Newtonsoft JSON for this purpose.

var clrs = JSON.parse('@Html.Raw(JsonConvert.SerializeObject(Model.AllKnownColors))');

Or you can write you own method for converting. Newtonsoft JSON is easy and good choice. Your own methods could be more fast.

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

2 Comments

Thanks you for your answer. For some reason, I get "JsonConvert does not exist in the current context.
You need add references to Newtonsoft.Json. You can make with help of nuget package: nuget.org/packages/Newtonsoft.Json.
0

Model.AllKnownColors is a string array. Html.Raw accepts a single string as an argument. Since Model.AllKnownColors is not a string, C# framework calls the object's ToString method. By default, ToString returns the name of the object type.

If you are attempting to use a pure string of colors, you can do something like this instead:

var clrs = '@string.Join(", ", Model.AllKnownColors)';

If you actually want to turn Model.AllKnownColors into a JavaScript array, you can do this:

var clrs = @Html.Raw(Json.Encode(Model.AllKnownColors));

Note that in both cases, JSON.parse is unnecessary.

7 Comments

The last solution gives me an error: Returns markup that is not html encoded :(
I forgot the semi-colon on the end of the second example. However, that should not cause invalid "markup" regardless.
What is the error specifically? I do not receive an error when I compile this code myself. Also, it may be helpful if you provide a larger code snippet of what you are trying to accomplish.
Raw() complained about the markup. I ignored it, and it seems like this actually gives me the data. :) Thanks.
Could you please tell why do I need json at all? I realized that can reach my model like this: @foreach(var color in Model.AllKnownColors)...
|

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.