views:

1063

answers:

2

Hi guys I'm very new to web app development and I thought I would start with recent technology and so I'm trying to learn asp.net aswell as the MVC framework design pattern at once. This is probably a very simple question for you mvc pros.

My question is should a partial view have an associated action, and if so, does this action get invoked whenever a normal page uses RenderPartial() on the partial view.

Thanks guys.

+13  A: 

While you can have an action that returns a partial view, you don't need an action to render a partial view. RenderPartial takes the partial view and renders it, using the given model and view data if supplied, into the current (parent) view.

You might want an action that returns a partial view if you are using AJAX to load/reload part of a page. In that case, returning the full view is not desired since you only want to reload part of the page. In this case you can have the action just return the partial view that corresponds to that section of the page.

Standard mechanism

Making use of partial view within a normal view (no action needed)

...some html...
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
...more html..

Ajax mechanism

Reloading part of a page via AJAX (note partial is rendered inline in initial 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
I see, I'm just playing around with the VS template for an MVC app. I am trying to display multiple lists of clients for instance within a partial view. I currently have a data transfer model class, how would I send this model to the partial view without involving the page view that renders this partial view?Thanks for your help!
yongjieli
The partial is always included in the main view. The only time you would return the partial on its own would be if you were updating via AJAX. Presumably you would use a partial to display **a** list of clients. You would, perhaps, use a foreach loop in your view to iterate over the lists (contained in the view model), passing each one to the partial as its model.
tvanfosson
+1 Very good! Nice answer.
Andrew Siemer
Wow very comprehensive answer, Thanks tvanfosson!
yongjieli
A: 

Hi, I am facing an issue with MVC, Can you please suggest a work-around Scott?

I have a partial view that is rendered at ...mysite/Home/Index and there is another button on the page which opens a pop up(modal) that contains this control again... now in this pop I modify the view model of this partial view and on close it is updated, the view model has been updated but the HTML on the page has not, I have tried JQuery and Ajax approaches to avoid page refresh/reload as I have unsaved data on the page but niether updated the HTML(seen on view page source as I need to perform edit operation on the appended HTML).

Thanks in advance

Best Regards

KK