views:

90

answers:

2

Hi there,

Can anyone help, I have got an Xdocument which opens an XML files on the disk and returns it to the view in asp.net mvc... Everything is working ok.

The problem being is that i need to manipulate the data with jquery, how do i pass this data which is asp.net mvc to jquery?

here is what i have

 XDocument xdoc = XDocument.Load(Server.MapPath("~/content/xml/items.xml"));
 var test = from f in xdoc.Descendants("categoria") select f;

 return view(test);

Basically the xml file is a list of items, so as the user clicks on a category then i display something in the right column, then someone clicks on another category on the item in the right column is replaced by new data. I have the code in jquery / javascript to do this...

All this data is available in my XML xdocument.. I could of course on the click event do a post to the server to a controller and return the new data .... but i want to be able to do this without any calls to the server

I think basically i need to store the xml file that i have in asp.net mvc into a javascript variable so that i can manipulate it with jquery..

Any help really appreciated

+2  A: 

Change the last line of your controller method to:

return Json(test);

Obviously, if you still need the original controller method, then make a new method which returns the JsonResult for use by your jQuery clients.

grenade
+1  A: 

I would return JSON from your controller as suggested by @grenade and then I would load this in a separate HTML page where you display and manipulate this data using JavaScript and jQuery.

This jQuery sample (with an MVC helper method) will load the data when the page has loaded:

$(document).ready(function() {
    $.getJSON('<%= Url.Action("JSonActionMethod") %>'), function(data) {
         // Do stuff with loaded JSON data stored in variable 'data'
    });
});

You'll need to make sure that Url.Action() points at the Action Method you have that is creating the JSON results page.

Hope that sets you on the right track and makes sence.

Richard