Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am 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

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


Jquery


enter code here
JavaScript
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. I want to load div with id two only

Someview contains





thanx in advance
Posted
Updated 27-Mar-13 21:23pm
v2
Comments
Zoltán Zörgő 28-Mar-13 3:25am    
Why not making a partial view for that div, and using ajax to fetch that content part only? What you do is not quite a good design.
Nitin Varpe 28-Mar-13 3:32am    
Actually project is quite big and i have used renderbody() for that page,and its not partial view,so cannot change view ,Cant i just retrieve a div from that view to replace original with the returned one.And thanx for your interest
Zoltán Zörgő 28-Mar-13 3:49am    
1) Extract it as partial view. It is not influencing the rest of your project.
2) By keeping your original idea, you incorporate a maintenance overhead, and a possible error source in your application
3) It works with jquery. Google can give you several examples, like this one: http://stackoverflow.com/questions/3300332/jquery-find-on-data-from-ajax-call-is-returning-object-object-instead or this one: http://forum.jquery.com/topic/get-dom-element-form-ajax-response
Nitin Varpe 28-Mar-13 6:03am    
Thanx for reply,Will check it

1 solution

Thanks A lot Zoltan,

Your links solved my problem

Solution


Quote:
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;
});
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900