Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC3
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
 
       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. I want to load div with id two only
 
Someview contains



thanx in advance
Posted 27-Mar-13 22:15pm
Edited 27-Mar-13 22:23pm
v2
Comments
Zoltán Zörgő at 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.
Member 8116353 at 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ő at 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
Member 8116353 at 28-Mar-13 6:03am
   
Thanx for reply,Will check it

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100