title: Populating a Webgrid from an Ajax Call
body: <p>I have a problem with populating a Webgrid from an Ajax call.</p>
  
  <p>I have followed the example as showed in the following thread: <a href="https://stackoverflow.com/questions/11351853/mvc-3-populate-bind-webgrid-form-ajax">mvc3 populating bind webgrid from ajax</a> however, that did not yield any results.</p>
  
  <p>When I run the website, I always get the message: "Error: undefined".</p>
  
  <p>when debugging the code, I am quite sure that the problem lies in the fact that the return PartialView is the problem, as my data object in the ajax success method does not get filled with data.</p>
  
  <p>Here are the examples of my code:</p>
  
  <p>Ajax call:</p>
  
  <pre><code>$.fn.getCardResult = function (leerling, kaart) {
          $.ajax({
              type: "GET",
              url: '@Url.Action("GetResults","Kaarten")',
              data: { leerlingID: leerling, cardID: kaart },
              cache: false,
              success: function (data) {
                  console.log(data);
                  if (!data.ok) {
                      window.alert(' error : ' + data.message);
                  }
                  else {
                      $('#card').html(data);
                  }
              }
          });
      }
  </code></pre>
  
  <p>Partial View call:</p>
  
  <pre><code>&lt;div class="card-content" id="card"&gt;
      @{
          if(Model.Kaart != null &amp;&amp; Model.Kaart.Count &gt; 0)
          {
              @Html.Partial("_Kaarten")
          }
          else
          {
              @: Er zijn geen kaarten beschikbaar.
          }
      }
  &lt;/div&gt;
  </code></pre>
  
  <p>Partial View:</p>
  
  <pre><code>@model List&lt;ProjectCarrousel.Models.KaartenModel&gt;
  
  @{
      var grid = new WebGrid(source: Model,ajaxUpdateContainerId: "card",
          defaultSort: "Topicname");
  
      grid.GetHtml(
          tableStyle: "webgrid",
          columns: grid.Columns(
              grid.Column("Topicname", "Topic"),
              grid.Column("Taskname", "Taken"),
              grid.Column("Taskpoints", "Punten"),
              grid.Column("Grades", "Resultaat"),
              grid.Column("Date", "Datum"),
              grid.Column("Teachercode", "Paraaf Docent")
          )
      );
  }
  </code></pre>
  
  <p>Controller code:</p>
  
  <pre><code>public ActionResult GetResults(int leerlingID, string cardID)
      {
          try
          {
              int Ovnumber = leerlingID;
              string CardId = cardID;
  
              List&lt;KaartenModel&gt; kaartlijst = new List&lt;KaartenModel&gt;();
  
  
              IEnumerable&lt;topic&gt; topics = _db.topic.Include("tasks.studenttotask").Where(i =&gt; i.CardID == CardId);
              foreach (topic topic in topics)
              {
                  foreach (tasks task in topic.tasks)
                  {
                      KaartenModel ka = new KaartenModel();
  
                      ka.Topicname = task.topic.Topicname;
                      ka.Taskname = task.Taskname;
                      ka.Taskpoints = task.Taskpoints;
                      ka.Ranks = task.Ranks;
                      ka.Date = task.studenttotask.Where(i =&gt; i.Ovnumber == Ovnumber).Select(d =&gt; d.Date).SingleOrDefault();
                      ka.Grades = task.studenttotask.Where(i =&gt; i.Ovnumber == Ovnumber).Select(d =&gt; d.Grades).SingleOrDefault();
                      ka.Teachercode = task.studenttotask.Where(i =&gt; i.Ovnumber == Ovnumber).Select(d =&gt; d.Teachercode).SingleOrDefault();
                      kaartlijst.Add(ka);
                  }
              }
  
              KVM.Kaart = kaartlijst;
  
              return PartialView("_Kaarten", KVM.Kaart);
  
          }
          catch (Exception ex)
          {
              return Json(new { ok = false, message = ex.Message });
          }
      }
  </code></pre>
  
  <p>If anyone could help it would be greatly appreciated.</p>
  
  <p><strong><em>UPDATE</em></strong></p>
  
  <p>After fiddling about a bit I found a solution that worked for me. Below is a snippet of an updated Ajax Call:</p>
  
  <p>The solution I found was too make the Success method in another way. This made sure that the Partial View rendered properly. Below is the Ajax call snippet.</p>
  
  <pre><code>$.ajax({
      url: '@Url.Action("GetAjaxCall","Home")',
      contentType: 'application/html; charset=utf-8',
      type: 'GET',
      dataType: 'html',
      data: { id: id },
  })
  .success(function (result) {
       $('#sectionContents').html(result);
   })
   .error(function (xhr, status) {
       alert(xhr.responseText);
   });
  </code></pre>