0

I'm converting a 2D object array into javascript, but I'm having trouble with the JSON serializer.

My controller has this:

object[,] array = new object[,] {{"Name", "Donuts eaten"},
                                          {"Michael", 5},
                                          {"Elisa", 7},
                                          {"Robert", 3},
                                          {"John", 2},
                                          {"Jessica", 6},
                                          {"Aaron", 1},
                                          {"Margareth", 8}};
ViewBag.DataArray =  JsonConvert.SerializeObject(array);

My script uses:

var dataArray = '@ViewBag.DataArray';

However, when the script is run, I get this:

var dataArray = '[["Name","Donuts eaten"],["Michael",5],["Elisa",7],["Robert",3],["John",2],["Jessica",6],["Aaron",1],["Margareth",8]]';

Any help or alternate methods would be appreciated. Thanks.

1
  • what do you want your output to be? Commented Apr 15, 2016 at 18:44

1 Answer 1

2

When you use @ symbol, razor will do an HTML encode on the result of the expression followed by @. If you do not want the content to be encoded, use Html.Raw method.

var dataArray = '@Html.Raw(ViewBag.DataArray)';

Now razor will produce the below output.

var dataArray = '[["Name","Donuts eaten"],["Michael",5],["Elisa",7],["Robert",3],
                                    ["John",2],["Jessica",6],["Aaron",1],["Margareth",8]]';
Sign up to request clarification or add additional context in comments.

Comments

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.