4

I'm using ASP.NET MVC (with Razor) and JQuery

I have a list of strings in my controller and I render the partial view passing in the model with the below list.

List<string> list = new List<string>();
list.Add("Texas");
list.Add("New York");

On client in my cshtml file side I have:

<div id = "test", test-att = @Html.Raw(Json.Encode(Model.list)) />

In my javascript file I do:

var javascriptArray = JSON.parse($('#test').attr('test-att'));

I get an error "unexpected end of input".

Using Chrome dev tools console I see the following:

$('#test') : <div id ="test" test-att = "["Texas", "New" York"]>

$('#test').attr('test-att') : "["Texas","New"

I'm expecting : "["Texas","New York"]"

Looking like its getting messed up because of the space before being passed in JSON.parse. It seems to stop when it finds a space.

Any ideas on how to fix this?

5
  • Why are you not just assigning it directly to the javascript variable? - var javascriptArray = @Html.Raw(Json.Encode(Model.list)) (or var javascriptArray = JSON.parse('@Html.Raw(Json.Encode(Model.list))');? Commented Aug 26, 2015 at 12:54
  • I can't do that. All my javascript logic is in the javascript file and I want to keep it that way. Commented Aug 26, 2015 at 12:58
  • Then you should at least be quoting it and preferably using data- attributes. <div id="test data-att="@Html.Raw(Json.Encode(Model.list))"> and get it using var javascriptArray = JSON.parse($('#test').data('att')); Commented Aug 26, 2015 at 13:01
  • Where does Json.Encode function come from? Commented Aug 26, 2015 at 13:02
  • Yeah tried that data-att as well. I get the same output. Commented Aug 26, 2015 at 13:06

4 Answers 4

7

Put your JSON between single quote NOT double quote characters:

<div id = "test" test-att = '@Html.Raw(Json.Encode(Model.list))' /> 
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, that worked! Can you please explain why that works with the single quotes. I also checked that it does NOT work with the double quotes. I will mark your reply as answer.
Because your JSON string already have double quote. And razor parser confuses with attributes quotes. By using single quote razor could easily distinguish between your JSON and html attribute.
0

Content of cshtml file will be like as bellow

<div id = "test" test-att = "@Html.Raw(Json.Encode(Model.list))" />

Comments

0

There seems to be 2 issues with what you have :

  1. The Json encoder uses " (double quotes) to delimitate strings. It's not gonna work since in XML double quote are used to delimitate attributes.

For this issue you have 2 solutions the easiest one would probably be replacing every " in the Json Encoded string by

<div id = "test", test-att ="@Html.Raw(Json.Encode(Model.list).Replace("\"","&quot;")" />

Comments

0

It seems to me the culprit is @Html.Raw. If you use the following -

<div id='dd' data-att="@Json.Encode(list)">

Then parsing with javascript doesn't fail.

var a = document.getElementById('dd');
JSON.parse(a.dataset.att);

Single quote or double quote doesn't matter. But if you use @Html.Raw then double quote gives you the said error. You can use @Html.Raw against each item of your list instead -

<div id='dd' data-att="@Json.Encode(list.Select(x=> Html.Raw(x).ToHtmlString()).ToList<string>())">

This will work with double quote.

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.