返回管窥作为JsonResult [英] Return Partial View As JsonResult

查看:141
本文介绍了返回管窥作为JsonResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有返回,获取通过Ajax调用追加到一个无序列表,列表项的HTML块的局部视图。这一切工作正常。

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.

但是,一旦我收到的HTML从AJAX调用回来,我想能够在每一个通过jQuery列表中的项目设置一些属性。为了做到这一点,我假设我需要收到AJAX调用的结果早在一个JsonResult,而不是一个ActionResult的。不幸的是,我想用我使用呈现HTML,因为它有相当多的包含在它的数据格式的ASCX保持。

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.

是否有可能呈现局部视图,然后将其转换为一个JsonResult为AJAX客户还是有不同的方法,我应该考虑的?

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?

任何意见是非常AP preciated。

Any advice is much appreciated.

谢谢!

推荐答案

您实际上不需要一个JsonResult。的部分是一个ActionResult获取一些XML / HTML吗?然后你可以使用jQuery来分析它,并查询它是这样的:

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
}

此外,如果你的结果只是一堆 s然后可以做到这一点的:

Also if your result is just a bunch of lis 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

这篇关于返回管窥作为JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆