title: How to convert View Model into JSON object in ASP.NET MVC?
body: <p>I am a Java developer, new to .NET. I am working on a .NET MVC2 project where I want to have a partial view to wrap a widget. Each JavaScript widget object has a JSON data object that would be populated by the model data. Then methods to update this data are bound to events when data is changed in the widget or if that data is changed in another widget.</p>
  
  <p>The code is something like this:</p>
  
  <p><code>MyController</code>:</p>
  
  <pre><code>virtual public ActionResult DisplaySomeWidget(int id) {
    SomeModelView returnData = someDataMapper.getbyid(1);
  
    return View(myview, returnData);
  }
  </code></pre>
  
  <p><code>myview.ascx</code>:</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;SomeModelView&gt;" %&gt;
  
  &lt;script type="text/javascript"&gt;
    //creates base widget object;
    var thisWidgetName = new Widget();
  
    thisWidgetName.updateTable = function() {
      //  UpdatesData
    };
    $(document).ready(function () {
      thisWidgetName.data = &lt;% converttoJSON(model) %&gt;
      $(document).bind('DATA_CHANGED', thisWidgetName.updateTable());
    });
  &lt;/script&gt;
  
  &lt;div&gt;&lt;%:model.name%&gt;&lt;/div&gt;
  </code></pre>
  
  <p>What I don’t know is how to send the data over as <code>SomeModelView</code> and then be able to use that to populate the widget as well as convert that to JSON. I had seen some real simple ways to do it in the controller but not in the view. I figure this is a basic question but I’ve been going for a few hours trying to make this slick.</p>