How to convert View Model into JSON object in ASP.NET MVC?
In mvc3 with razor @Html.Raw(Json.Encode(object))
seems to do the trick.
Well done, you've only just started using MVC and you've found its first major flaw.
You don't really want to be converting it to JSON in the view, and you don't really want to convert it in the controller, as neither of these locations make sense. Unfortunately, you're stuck with this situation.
The best thing I've found to do is send the JSON to the view in a ViewModel, like this:
var data = somedata;
var viewModel = new ViewModel();
var serializer = new JavaScriptSerializer();
viewModel.JsonData = serializer.Serialize(data);
return View("viewname", viewModel);
then use
<%= Model.JsonData %>
in your view. Be aware that the standard .NET JavaScriptSerializer is pretty crap.
doing it in the controller at least makes it testable (although not exactly like the above - you probably want to take an ISerializer as a dependency so you can mock it)
Update also, regarding your JavaScript, it would be good practice to wrap ALL the widget JS you have above like so:
(
// all js here
)();
this way if you put multiple widgets on a page, you won't get conflicts (unless you need to access the methods from elsewhere in the page, but in that case you should be registering the widget with some widget framework anyway). It may not be a problem now, but it would be good practice to add the brackets now to save yourself muchos effort in the future when it becomes a requirement, it's also good OO practice to encapsulate the functionality.