1

I am trying to serialize my linq to JSON. My issue is Json result wrapped in pre tag - how to get value of it. The answer is not what I am looking for though. Here are my code Controller

return Json(regionBoudaries, JsonRequestBehavior.AllowGet);

I see that my JSON string is written in the page Now I am writing something like something like

View

$(document).ready(function () {        
    initialize(); 
    process(a_variable);
}

How can I set the a_variable the value of the JSon returned from controller. Please help me out. Thank you in advance

1
  • Can you tell us how you are calling your controller action which returns the JSON? Usually we see a pattern where one controller action is called to render a view, and then some client javascript code in that view calls another controller action to retrieve and process some JSON. It looks like you maybe are trying to do this in one step rather than two steps? Commented Apr 7, 2013 at 15:58

1 Answer 1

1

You could use a view model:

public class MyViewModel
{
    public class SomeType RegionBoudaries { get; set; }

    ... some other properties
}

and then have the controller action serving this view populate the property of the view model:

public ActionResult SomeAction()
{
    var model = new MyViewModel();
    model.RegionBoudaries = ... same stuff as in your other action
    return View(model);
}

and then in the corresponding strongly typed view:

@model MyViewModel
...
<script type="text/javascript">
    $(document).ready(function () {        
        var a_variable = @Html.Raw(Json.Encode(Model.RegionBoudaries));
        initialize(); 
        process(a_variable);
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.