[ACCEPTED]-asp.net MVC partial view controller action-html.renderpartial

Accepted answer
Score: 139

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

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

Standard mechanism

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

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

Ajax mechanism

Reloading 3 part of a page via AJAX (note partial is 2 rendered inline in initial page load)

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

<script type="text/javascript">
   $(function() {
       $('#someButton').click( function() {
              url: '/controller/action',
              data: ...some data for action...,
              dataType: 'html',
              success: function(data) {

Controller 1 for AJAX

public ActionResult Action(...)
     var model = ...


     if (Request.IsAjaxRequest())
          return PartialView( "Partial", model.PartialModel );
          return View( model );
Score: 4

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

Score: 4

I was able to achieve something similar 4 with this logic.

Within the .cshtml

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

Within 3 the controller

public ActionResult ActionMethodName()
    var someModel = new SomeModel();
    return PartialView("SomeView.cshtml", someModel);

And that's it.

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

Score: 2

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

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

The actionmethod 3 can look like:

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


and the view 'StockWarningsPartial.cshtml' starts 2 with:

    Layout = null;

to make it not render your surrounding 1 layout again.

Score: 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");


More Related questions