I'm trying to load values from my database to a DataTable object, to an array.
I'm then trying to pass that array to jQuery DataTables plug in, however, I can't get jQuery to hit my method using $.getJSON.
I just tried this for another plugin and it worked, except not an array.
public ActionResult GetAssociateFromDb()
{ // HomeController.cs
DataTable dt = new DataTable();
string jsonData;
string connString = ConfigurationManager.ConnectionStrings["DEFCOMP"].ConnectionString;
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = connString;
using (var cmd = new SqlCommand("SELECT * FROM FUND", connection))
{
connection.Open();
var myAdapter = new SqlDataAdapter(cmd);
myAdapter.Fill(dt);
var arr = new ArrayList();
foreach ( DataRow dr in dt.Rows)
{
arr.Add(dr);
}
return Json(arr, JsonRequestBehavior.AllowGet);
//Debugging, arr has Count = 20.
}
}
}
This is the code in my view.
$(document).ready(function() {
$.getJSON('@Url.Action(actionName: "GetAssociateFromDb", controllerName: "Home")', function (data) {
alert(JSON.stringify(data)); // I never get an alert
// $('#example').dataTable();
});
});
Failed to load resource 404 and 500 errors.