0

I send json string to the view using viewdata in my mvc , c# project. this is in my controller

string getregionsforSearch = csv2json.returnJsonstringFull(getregionsPath);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            ViewData["JsonRegionList"] = serializer.Serialize(getregionsforSearch);

now I want to retrieve it in my view inside

<script></script>

and query it.how can I do that.

1
  • 1
    Data passed from the controller to the view should be kept on the ViewModel it pains me to see why people use the ViewData store. Commented Apr 1, 2016 at 8:42

1 Answer 1

1

Use Json.Encode() and @Html.Raw() as shown :-

<script>
     var data = @Html.Raw(Json.Encode(ViewData["JsonRegionList"]))
</script>
Sign up to request clarification or add additional context in comments.

9 Comments

where to put Json.Encode() . in thre controller or in the view
@bill..as you can see i have written the code inside script so it is obvious that it will be in view inside script tags.
no what is the namespace for Json.Encode is it gives an error
its inside System.Web.Helpers namespace.
@bill...its inside System.Web.Helpers namespace
|

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.