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>
5
  • 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. Commented Sep 26, 2013 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 Commented Sep 26, 2013 at 17:08
  • OIC, consider using a partial view with child action for your report. Then request a new one with JQuery .load() Commented Sep 26, 2013 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? Commented Sep 26, 2013 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. Commented Sep 26, 2013 at 23:17

1 Answer 1

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>';
Sign up to request clarification or add additional context in comments.

2 Comments

How can I put my serialized data(employees) on a var(yourJason)? that I can call from my jquery
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 ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.