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:
Adding System.Web.Extensions.
Copying System.Web.Extensions locally (in the Properties on Visual Studio 2013)
Removing & Re-Adding System.Web.Extensions (and cleaning & rebuilding and closing & re-opening VS).
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.