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

XML
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

SQL
[ChildActionOnly]
       public PartialViewResult nav_partial()
       {

           return PartialView();
       }
    

       [ChildActionOnly]
       public PartialViewResult nav_partial_large()
       {

           return PartialView();
       }




Pls help me

Thanks,
Soumya
Posted

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

SQL
public ActionResult partialview1()
        {


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


          return PartialView();
      }
 
Share this answer
 
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.
 
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