Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
Hi All,
 
I want to load the partial views according to the condition here with the following code it's not working
 
my jquery fn
 
var ww = document.body.clientWidth;
 
$(document).ready(function () {
 
    if (ww < 768) {
 

        $('<div>').appendTo('#div_partial').load('Html.RenderPartial("nav_partial", "Mydemo");');
    }
    else {
 
        $('<div>').appendTo('#div_partial').load('Html.RenderPartial("nav_partial_large", "Mydemo");');
    }
})
 

Here is my controller
 
[ChildActionOnly]
       public PartialViewResult nav_partial()
       {
 
           return PartialView();
       }
    
 
       [ChildActionOnly]
       public PartialViewResult nav_partial_large()
       {
 
           return PartialView();
       }
 

 
Pls help me
 
Thanks,
Soumya
Posted 12-Dec-12 4:14am
soumyaraj1.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Html.RenderPartial is server side code. You would need a @ in front of it, and you in general need to have your js contain the final HTML, not a server side call. An AJAX call to get the HTML makes the most sense IMO.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In js file
 
if (condition) {
$.ajax({
cache: false,
type: 'GET',
url: '/demo/partialview1',
data: null,
success: OnSuccessCall,
error: OnErrorCall
});

}
else {
$.ajax({
cache: false,
type: 'GET',
url: '/demo/partialview2',
data: null,
success: OnSuccessCall,
error: OnErrorCall
});
 
}
 
})
 
var OnSuccessCall = function (response) {
 
$('#divname').html(response.valueOf(0))
}
var OnErrorCall = function () {
 
alert(request.status + " " + request.statusText);
 
}
 
In controller
 
public ActionResult partialview1()
        {
 

          return PartialView();
         }
      public ActionResult partialview2()
      {
 

          return PartialView();
      }
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 13 Dec 2012
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