Reputation: 3099
I have a partial view that returns an HTML chunk of list items that gets appended onto an unordered list via an AJAX call. This all works fine.
However, once I receive the HTML back from the AJAX call, I would like to be able to set some properties on each of the list items via JQuery. In order to do that, I'm assuming that I need to receive the results of that AJAX call back as a JsonResult rather than a ActionResult. Unfortunately, I want to keep using the ASCX that I'm using to render the HTML since it has quite a bit of formatting data contained within it.
Is it possible to render a partial view and then convert it to a JsonResult for the AJAX client or is there a different approach that I should consider?
Any advice is much appreciated.
Thanks!
Upvotes: 1
Views: 1778
Reputation: 38860
You dont actually need a JsonResult. A partial that is an ActionResult retrieves some xml/html right? Then you can use jquery to parse it and query it like this:
function success(result) {
var html = $(result); //this creates a jquery object out of your result html
$("ul li", html); //this gets you all the list items in context of the resulting html
}
Also if your result is just a bunch of li
s then you can do this:
function success(result) {
var lis = $(result);
lis.each(function() { $(this).append(" testing"); });
$("#yourUl").append(lis);
}
This can be simplified with chaining but i made it a bit more verbose so its easier to understand
Upvotes: 4