Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Div from returned view

I an working on returning view from controller to jquery ,View is returned but i want to extract div from returned view.My Current code is like this

       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>

My current logic returns view and assign total view i.e html+body to body part of page ,which shows html part two times.Is there any way to retrieve div from the returned view and reload it. thanx in advance

like image 250
Nitin Varpe Avatar asked Nov 27 '25 16:11

Nitin Varpe


1 Answers

Your controller action should return a PartialViewResult otherwise it will return your layout page in the response. If you want to cater for both scenarios you can check whether the request is an AJAX request:

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");
}

To understand the difference between returning View and returning PartialView please see What's the difference between "return View()" and "return PartialView()".

like image 77
Ben Foster Avatar answered Nov 30 '25 10:11

Ben Foster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!