Question

I am using ASP.NET MVC 3 with the razor view engine.

I have the following method in my NewsController:

public JsonResult GetAllNews()
{
   var items = newsService.FindAll();
   var jsonResult = Json(items);

   return jsonResult;
}

In my view I want to try and call this method to populate my YUI datatable. I put a breakpoint on the first line of this method but the breakpoint is not hit. Here is my code in the view to call this method:

var newsDataSource = YAHOO.util.DataSource('@Url.Action("GetAllNews");');

I even tried:

var newsDataSource = YAHOO.util.DataSource("/News/GetAllNews/");

Both don't seem to work.

Here is my datatable code:

<div id="grdNews"></div>

<script type="text/javascript">
   // News grid
   var newsColumnDefs = [
      { key: "id", label: "Identifier" },
      { key: "title", label: "Title" },
      { key: "body", label: "Body" }
   ];

   //var newsDataSource = YAHOO.util.DataSource('@Url.Action("GetAllNews");');
   var newsDataSource = YAHOO.util.DataSource("/News/GetAllNews/");
   newsDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
   newsDataSource.responseSchema = {
      fields: [
         { key: "id" },
         { key: "title" },
         { key: "body" }
      ]
   };

   var myDataTable = new YAHOO.widget.DataTable("grdNews", newsColumnDefs, newsDataSource);
</script>

What am I doing wrong?

Was it helpful?

Solution

Don't forget to make this method return JSON for GET requests as well:

public JsonResult GetAllNews()
{
   var items = newsService.FindAll();
   return Json(items, JsonRequestBehavior.AllowGet);
}

Also setting a datasource doesn't mean that it will invoke the method. Maybe there's some other part of your code that's problematic. Install FireBug and see if an AJAX request is sent.


UPDATE:

Now that the question has been clarified and you are talking about YUI datatable, here's a full working example:

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GetAllNews()
    {
        var news = new[] 
        {
            new { id = 1, title = "title 1", body = "body 1" },
            new { id = 2, title = "title 2", body = "body 2" },
            new { id = 3, title = "title 3", body = "body 3" },
        };
        return Json(new
        {
            Result = news
        }, JsonRequestBehavior.AllowGet);
    }
}

View (~/Views/Home/Index.cshtml):

@{
    ViewBag.Title = "Home Page";
}

<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/fonts/fonts-min.css" /> 
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/datatable/assets/skins/sam/datatable.css" /> 
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js"></script> 
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/connection/connection-min.js"></script> 
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/json/json-min.js"></script> 
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/element/element-min.js"></script> 
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/datasource/datasource-min.js"></script> 
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/datatable/datatable-min.js"></script> 

<script type="text/javascript">
    var newsColumnDefs = [
        { key: "id", label: "Identifier" },
        { key: "title", label: "Title" },
        { key: "body", label: "Body" }
    ];

    var newsDataSource = new YAHOO.util.DataSource('@Url.Action("GetAllNews")');
    newsDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
    newsDataSource.responseSchema = {
        resultsList: 'Result',
        fields: [ "id", "title", "body" ]
    };

    var myDataTable = new YAHOO.widget.DataTable("grdNews", newsColumnDefs, newsDataSource); 
</script>

<div id="grdNews"></div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top