80

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 as-well as the MVC framework at once. This is probably a very simple question for you, MVC professionals.

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
yogibear
  • 14,487
  • 9
  • 32
  • 31

5 Answers5

140

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
  • 524,688
  • 99
  • 697
  • 795
  • 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! – yogibear Sep 03 '09 at 01:51
  • 1
    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 Sep 03 '09 at 01:54
  • Wow very comprehensive answer, Thanks tvanfosson! – yogibear Sep 03 '09 at 02:04
  • One advice is to use Url.Action because hardcoding the path like this will break the application on route changes or map structure changes. – Jean-Paul Oct 21 '16 at 14:26
4

The accepted answer is completely correct, but I want to add that you can load your partial view using jQuery load. Less configuration needed, if you don't want to consider concurrency.

$("#Your-Container").load("/controller/action/id");
Saeid
  • 1,573
  • 3
  • 19
  • 37
4

I was able to achieve something similar with this logic.

Within the .cshtml

@Html.Action("ActionMethodName", "ControllerName");

Within the controller

[Route("some-action")]
public ActionResult ActionMethodName()
{
    var someModel = new SomeModel();
    ...
    return PartialView("SomeView.cshtml", someModel);
}

And that's it.

If you need to pass values from the .cshtml to the action method then that is possible to.

Bern
  • 7,808
  • 5
  • 37
  • 47
  • 1
    Perfect example. You showed the interaction and code in both the controller and view in a clean and clear straight to the point manner. With out the fluff. Thank you. – eaglei22 Oct 10 '17 at 21:37
2

The answer is no. But sometimes you need some controller action behind a partial view. Then you can create an actionMethod wich returns a partial view. This actionMethod can be called within another view:

@Html.Action("StockWarningsPartial", "Stores")

The actionmethod can look like:

public ActionResult StockWarningsPartial()
{
      ....              
      return View("StockWarningsPartial", warnings);

}

and the view 'StockWarningsPartial.cshtml' starts with:

@{
    Layout = null;
}

to make it not render your surrounding layout again.

Peter
  • 489
  • 3
  • 16
0
 public ActionResult GetStateList(int country_id)
 {
      List<stateDTO> stateList = new List<stateDTO>();
      stateList = bll.GetState(country_id);
      ViewBag.sList = new SelectList(stateList, "state_id", "State_Name");
      return PartialView("DisplayStates");
 }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 06 '22 at 08:57