While you may have an action that returns a partial view, you do not need an action to partially view it. RenderPartial takes a partial view and displays it using this model and displaying the data, if presented, in the current (parent) view.
You may need an action that returns a partial view if you use AJAX to load / reload part of the page. In this case, returning the full view is undesirable since you only want to reload part of the page. In this case, you can force the action to simply return the partial view corresponding to this section of the page.
Standard gear
Using partial view in normal mode (no action required)
...some html... <% Html.RenderPartial( "Partial", Model.PartialModel ); %> ...more html..
Ajax mechanism
Reloading part of a page using AJAX (a note that partially displays in the original page load)
...some html... <div id="partial"> <% Html.RenderPartial( "Partial", Model.PartialModel ); %> </div> ...more html... <script type="text/javascript"> $(function() { $('#someButton').click( function() { $.ajax({ url: '/controller/action', data: ...some data for action..., dataType: 'html', success: function(data) { $('#partial').html(data); }, ... }); }); }); </script>
Controller for AJAX
public ActionResult Action(...) { var model = ... ... if (Request.IsAjaxRequest()) { return PartialView( "Partial", model.PartialModel ); } else { return View( model ); } }
tvanfosson Sep 03 '09 at 1:41 2009-09-03 01:41
source share