2

How can I read C# dictionary to Javascript in MVC3 without Razor? I can do this fine with newer MVC with

var texts = @Html.Raw(JsonConvert.SerializeObject(Model.TextDict));

But my older project does not recognize Jsoncovert. I tried with:

var texts = <% Html.Raw(Json.Encode(Model.TextDict)); %>

But here the texts is undefined.

3
  • 1
    Add a Json.Net reference, preferable via NuGet to get JsonConvert.SerializeObject. Commented Apr 23, 2018 at 12:48
  • 1
    I support @UweKeim, you are going to save a lot of time and effort. Commented Apr 23, 2018 at 12:51
  • That will be done later when complete project is lifted to newer version. I got this working now with Ludovics help. Commented Apr 23, 2018 at 12:58

2 Answers 2

3

That because <% ... %> simply execute the code inside and doesn't return anything. You should use <%= ... %> instead. Try this:

var texts = <%= Html.Raw(Json.Encode(Model.TextDict)); %>
Sign up to request clarification or add additional context in comments.

Comments

1

use

var texts = @Html.Raw(JsonConvert.SerializeObject(Model.TextDict));

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.