Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial rendering after page loaded

I have a page that contains some usercontrols. I would like to load these usercontrols after postback like an ajax rendering.

Each usercontrols display a list from database and I dont want the user to wait while server code builds up the response I think it will be useful if the page is displayed for the user and after the usercontrols are loaded through an ajax request.

Is there an exist solution in ASP.NET MVC? Is there an exist solution for this problem?

thanks advance: l.

like image 365
user295541 Avatar asked Dec 21 '10 11:12

user295541


2 Answers

Just use jQuery to bind the HTML returned from the action method (which should return a partial view result - e.g the output of the user control/partial):

Controller:

[HttpGet]
public PartialViewResult GetSomeData()
{
   var data = somewhere.GetSomething();
   return PartialView(data); // partial view should be typed to data.
}

jQuery:

$(document).ready(function() {
   $.get('/home/getsomedata/', function(data) {
      $('#target').html(data);
   });   
});
like image 170
RPM1984 Avatar answered Nov 16 '22 00:11

RPM1984


I usually do this way:

In the markup I reserve space for the user control to be loaded like

<div id="i-tabs-5">
    <div style="text-align:right;margin-bottom:6px;">...</div>
    <div id="issueNoteListPlaceholder"></div>
</div>

then on DOM Ready I make an ajax call that return a partial view result and replace the content of the placeholder

$(document).ready(function () {
    loadIssueNotes();
});

function loadIssueNotes() {
    $.ajax({
        type: "get",
        dataType: "html",
        url: '<%: Url.Content("~/Controller/Action") %>',
        data: {},
        success: function (response) {
            $("#issueNoteListPlaceholder").html('').html(response);
        }
    });
}
like image 3
Lorenzo Avatar answered Nov 15 '22 22:11

Lorenzo