3

我正在将视图从控制器返回到 jquery,返回视图但我想从返回的视图中提取 div。我当前的代码是这样的

       public ActionResult DeleteItem(int pid)
                {
                 //my logic goes here
                    retutn View("SomeView",model);
                    }

                 Jquery


enter code here
          script type="text/javascript">
              $(document).ready(function () {

        $('.Remove').click(function () {
             var value = $(this).attr('id');
            $.ajax({
                cache:true,
                type: "POST",
                url: "@(Url.Action("DeleteItem", "ControllerName"))",
                data: "pid=" + value,
                success: function (data) {
                    $("body").html(data);

                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('Failed to subscribe.');

                }, 
                complete: function() {   } 
           });       
            return false;
                 });
                      });

        </script>

我当前的逻辑返回视图并将总视图即 html+body 分配给页面的正文部分,它显示了 html 部分两次。有没有办法从返回的视图中检索 div 并重新加载它。提前谢谢

4

2 回答 2

1

您的控制器操作应返回 aPartialViewResult否则它将在响应中返回您的布局页面。如果您想同时满足这两种情况,您可以检查请求是否为 AJAX 请求:

public ActionResult DeleteItem(int id) {
    // delete your item

    if (Request.IsAjaxRequest()) {
        // return just the partial view
        return PartialView("yourview");
    }


    // otherwise handle normally
    return RedirectToAction("list");
}

要了解退货和退货的区别,请参阅“return View()”和“return PartialView()”有什么View区别。PartialView

于 2013-03-28T23:57:02.293 回答
0
      script type="text/javascript">
          $(document).ready(function () {

    $('.Remove').click(function () {
         var value = $(this).attr('id');
        $.ajax({
            cache:true,
            type: "POST",
            url: "@(Url.Action("DeleteItem", "ControllerName"))",
            data: "pid=" + value,
            success: function (data) {
                     var t=$(data).find('.divtoreplacewith');
                               $('.divtoreplace').replaceWith(d);
         //Wasted my 2 days for above two lines.But satisfied with solution,
          //Both div are same but '.divtoreplacewith' is having new data,And I have replaced div with that div that's all
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert('Failed to subscribe.');

            }, 
            complete: function() {   } 
       });       
        return false;
             });
                  });
于 2013-03-29T06:13:30.420 回答