2

I declared and serialized data on my View page

@model IEnumerable<MVCvs2012.Models.Employees>

 @{
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var employees = @serializer.Serialize(ViewData["Employees"]);   
  }

Results when running the browser:

[{"FirstName":"Nancy","LastName":"Davolio","Title":"Sales Representative"}]

I want to present this data using jquery into a table like this:

<table id="employee">
<tr>
  <td>Nancy</td>
  <td>Davolio</td>
  <td>Sales Representative</td>
</tr>
...
</table>
David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
Francis Padron
  • 307
  • 2
  • 7
  • 15
  • Im lost why you are using JSON format to send C#/Razor data. Seems more sensible to pass your list to your view and render it with a for each loop. – Dave Alperovich Sep 26 '13 at 16:58
  • Yes, I tried that method and it is a 100% , but I'm trying to find a way to display those data without refreshing the page and by just clicking on a button, you can have those data appended to a table in your view – Francis Padron Sep 26 '13 at 17:08
  • OIC, consider using a partial view with child action for your report. Then request a new one with JQuery `.load()` – Dave Alperovich Sep 26 '13 at 20:24
  • Correct me if I'm wrong, There's a caching going on when using Partial View? and using jquery .load() you can get those cached data and store them on a var or something? – Francis Padron Sep 26 '13 at 21:27
  • when you use a jquery `.load()`, it overwrites caching. I use .load() with partials for all incremental behaviors. Can be used to refresh a report or to add more data by passing a parameter. – Dave Alperovich Sep 26 '13 at 23:17

1 Answers1

1

Use jQuery.each to build your table. Something along the lines of

$var table = '<table>';
$.each(yourJson, function( index, value ) {
      $table += '<tr><td>' + value.FirstName + '</td><td>' + ...
});
$table += '</table>';
Linell
  • 750
  • 3
  • 9
  • 27
  • How can I put my serialized data(employees) on a var(yourJason)? that I can call from my jquery – Francis Padron Sep 26 '13 at 17:24
  • I haven't used MVC in a while, _but_ you just need to store it as some sort of variable, then you can put it in. So like var data = @GetItFromYourController, then just call it as $.each(data .. – Linell Sep 26 '13 at 17:57