views:

374

answers:

1

Hey all. Okay here is the situation. I would like to be able to load a series of similar partial views onto a parent page; much in the same way as is being done in this article (simulating AJAX panels).

In the article, it appears to be working fine, as it is calling the corresponding action method asynchronously on the controller. But the method he is using looks like a hack at best, and doesn't seem very elegant. A brief overview of what is going on is:

This would be an independent widget that loads asynchronously.

<% using (Ajax.Form("ReportOne", null,
      new AjaxOptions { UpdateTargetId = "resultOne" },
      new { id="reportFormOne" })) { } %>

<script type="text/javascript">
   $get("reportFormOne").onsubmit();
</script>

The corresponding output would be of the form:

<form action="/Home/ReportOne/reportFormOne" onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, { insertionMode: 0, updateTargetId: 'resultDiv' }); return false;">

   <!-- Your form elements here... -->

</form>

Like I had stated above, this doesn't seem like a really elegant way to go about achieving this, but I've been trying to find an alternative for this (using jQuery or other means), and have been unsuccessful in doing so.

+1  A: 

jQuery solution: loads the html from the action after page load is complete.

<script type='text/javascript'>
$(function() {
    $.load('<%= Url.Action( "ReportOne", "Home", new { id = "reportFormOne" } ) %>', null, function(response,status,xhr) {
        $('#partialView').html(reponse);
    });
});
</script>

...

<div id='partialView'>
</div>
tvanfosson
Thank you. This would work great; however, if I'm needing to add multiple partial views with similar data, does it make sense to use jQuery to get the id's of each of the widgets from the DB that I need to load and then loop through each calling the action? This seems to be relying on jQuery an awful lot.
You could generate all this in ViewData (or your Model) and then dynamically build the jQuery code to get just those specific items that you need. This would simplify the client-side code that's required.
tvanfosson