1

I am currently trying to display one of my views (partial view) inside of another one. Inside of my view, I have been using the following code: @{ Html.RenderPartial("_Pending"); }.

My partial view is inside of my Shared folder. My goal is to pull in two different views using logic from two different controllers. Given my two views (Transaction Index and Pending Transactions (partial)), I essentially want both of these to appear on the same page. The issue is that currently, I am essentially getting the same view twice.

In short, how do I display a partial view inside of another view, with the partial view returning results from a different controller?

2 Answers2

3

If I understand correctly, what you want is getting result of same partial view but executed by two different controller.

Have you tried @Html.Action?

essentialy it is the same concept as calling Partial but with controller action involved.

to use it, just create two Action then call @Html.Action in your view, such as:

in your controller:

public ActionResult Action1() {
    return PartialView("__Pending");
}

public ActionResult Action2() {
    return PartialView("__Pending");
}

in your view - you can also call it within your partial view

@Html.Action("Action1")
@Html.Action("Action2")

More info abount Partial and Action difference check out

MVC Html.Partial or Html.Action

more info about hwo to use Html.Action
How can I use Html.Action?

Community
  • 1
  • 1
Alan Tsai
  • 2,465
  • 1
  • 13
  • 16
  • Glad you have resolved it. Just a FYI, RenderAction and Action are essentially the same only the former write directly to response, where as latter returns html string. – Alan Tsai May 02 '17 at 07:22
0

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. Find an example to use many partial views inside one view. In aspx page:-

<asp:content id="content" contentplaceholderid="CenterContentPlaceHolder" runat="server">
<form id="frmCheckout" name="frmCheckout" method="post">
<div class="rzccartcont">                     

                  <%this.Html.RenderPartial("CheckoutProduct", Model);%>

                     <div class="rzcchkpmnt rzcchkpmnt-bg" id="divChkbottomArea">
                         <% this.Html.RenderPartial("CheckoutCartProfileInformation", Model); %>

                         <%this.Html.RenderPartial("CheckoutCartPaymentDetails", Model); %>

                     </div>
</div>
</form>
</asp:content>

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

<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 );
     }
}

Hope it helps you.

Vinutha N
  • 156
  • 6