Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering Partial Views using ajax

Tags:

I've checked this question and it solved my initial problems. But I don't want the partial view to be rendered only when the user clicks a link, I want to render partial views when the page is loaded and, possibly, show a progress indicator while the partial view is being loaded.

How to achieve that?

Thank you very much for reading this.

like image 465
programad Avatar asked Sep 15 '11 12:09

programad


1 Answers

If you want to load the page and then load the partial view via ajax you could create an ActionMethod that does something like:

public ActionResult Create() {      var model = new MyModel();       if (Request.IsAjaxRequest())      {           return PartialView( "_Partial", model.PartialModel );      }      else      {           return View( model );      }  } 

and then in your page do something like:

$(function(){      $(document).ajaxStart(function () {         //show a progress modal of your choosing         showProgressModal('loading');     });     $(document).ajaxStop(function () {         //hide it         hideProgressModal();     });      $.ajax({           url: '/controller/create',           dataType: 'html',           success: function(data) {              $('#myPartialContainer').html(data);           }     }); }); 
like image 91
ericb Avatar answered Dec 10 '22 16:12

ericb