Asp.net MVC: Creating Xdocument for jquery?

Can someone help, I have an Xdocument that opens the XML files on disk and returns it to the view in asp.net mvc ... Everything works fine.

The problem is that I need to manipulate data using jquery, how do I pass this data, which is asp.net mvc for jquery?

that's 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, an XML file is a list of elements, as the user clicks on a category, and then I show something in the right column, and then someone clicks on another category in the element in the right column, it is replaced with new data. I have code in jquery / javascript to do this ...

All of this data is available in my xdocument XML document. I could, of course, make a message to the controller on the click event and return new data ... but I want this to be possible without any calls to the server

I think that basically I need to save the XML file that I have in asp.net mvc into a javascript variable so that I can manipulate it using jquery ..

Any help really appreciated

+3
source share
2 answers

Change the last line of your controller method to:

return Json(test);

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

+2
source

JSON , @grenade, HTML, JavaScript jQuery.

jQuery ( MVC) :

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

, Url.Action() , , JSON.

, .

+1

Source: https://habr.com/ru/post/1725827/


All Articles