1

I'm trying to create a simple graph in MVC. I'm using Google's graph API, and I'm getting an error when I try to call a function in my controller to get data.

Here is my javascript:

jQuery.getJSON('/Home/LoadJSONData', function (data) {
    var data = new google.visualization.DataTable(data);
    var options = { 'title': 'Test Chart', 'width': 400, 'height': 300 };
    var chart = new google.visualization.ScatterChart(document.getElementByID('MyDiv'));
    chart2.Draw(data, options);
});

And here is the function I'm calling:

[HttpGet]
public JsonResult LoadJSONData()
{          
    //LoadData() returns a datatable from my database
    DataTable _dt = LoadData(); 

    //create a list of the models I want
    List<MyModel> ModelList = new List<MyModel>();

    foreach (DataRow _dr in _dt.Rows)
    {
        //Call the constructor using the datarow and add the new model to my list
        ModelList.Add(new MyModel(_dr));
    }    

    return Json(ModelList, JsonRequestBehavior.AllowGet);
}

When I run my code, I can step through the LoadJSONData() function, and when I step past the return function I get an error: JavaScriptSerializer.cs not found

Here are my includes in my controller:

using System.Web;
using System.Web.Script;
using System.Web.Script.Serialization;

I'm using .NET 4.5.

I have tried the following steps:

  1. Adding System.Web.Extensions.

  2. Copying System.Web.Extensions locally (in the Properties on Visual Studio 2013)

  3. Removing & Re-Adding System.Web.Extensions (and cleaning & rebuilding and closing & re-opening VS).

  4. Copying the System.Web.Extensions.dll from the Reference Assemblies folder to my bin folder for my project.

Additionally, I can add the following line to my LoadJSONData() function, right before the return statement:

JavaScriptSerializer js = new JavaScriptSerializer();

This compiles and runs fine, and I still get the same error after the return statement.

I'm pretty new to MVC and pretty bad at Javascript so it's very possible I'm doing something fundamentally wrong - but this seems correct to me and all my searching for this error has only lead me to fiddle with the System.Web.Extensions reference.

Edit: Here is my stack trace:

Locating source for 'f:\dd\ndp\fx\src\xsp\System\Extensions\Script\Serialization\JavaScriptSerializer.cs'. (No checksum.)
The file 'f:\dd\ndp\fx\src\xsp\System\Extensions\Script\Serialization\JavaScriptSerializer.cs' does not exist.
Looking in script documents for 'f:\dd\ndp\fx\src\xsp\System\Extensions\Script\Serialization\JavaScriptSerializer.cs'...
Looking in the projects for 'f:\dd\ndp\fx\src\xsp\System\Extensions\Script\Serialization\JavaScriptSerializer.cs'.
The file was not found in a project.
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src\vccorlib\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\src\mfc\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\src\atl\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: f:\dd\ndp\fx\src\xsp\System\Extensions\Script\Serialization\JavaScriptSerializer.cs.
The debugger could not locate the source file 'f:\dd\ndp\fx\src\xsp\System\Extensions\Script\Serialization\JavaScriptSerializer.cs'.

Which doesn't really look like a stack trace does it?

Edit 2:

If I took off my break point and just skipped over the step that was erroring... I stopped getting my error? The code ran just fine and would let me hit break points after the point that was "crashing". So it's not much of a resolution but I guess it's something? If you land on this page from the depths of google then try moving your breakpoints around.

Max
  • 849
  • 9
  • 24
  • This is a strange error, because that's part of the framework. Anyways, as an alternative you may wish to try [JSON.NET](http://www.newtonsoft.com/json). It's the most popular .NET JSON library. You can [tell MVC to use that instead](http://stackoverflow.com/questions/14591750/asp-net-mvc4-setting-the-default-json-serializer). – mason Apr 07 '15 at 13:29
  • What is the full stack trace for that exception? – Rubens Farias Apr 07 '15 at 13:30
  • When you added `System.Web.Extensions` reference, did you make sure it was the correct framework version? – mason Apr 07 '15 at 13:33
  • 1
    @RubensFarias, I added the "stack trace" to my post. It's not really a normal stack trace though. – Max Apr 07 '15 at 14:15
  • @mason, I'll check out that library. I added the reference through VS2013, I'm adding version 4.0.0.0 (runtime version 4.0.30319). – Max Apr 07 '15 at 14:16
  • @user2291983 - That looks more like the debug output rather than a stack trace. – phuzi Apr 07 '15 at 16:29
  • @mason3 Although Json(object, JsonRequestBehaviour) is normally part of the framework it's possible it's been overridden. A lot of the time it's done to customise the way serialisation is done. Try stepping in to the last line - it may not be framework code. – phuzi Apr 07 '15 at 16:31

0 Answers0