Reputation: 139
I have a situation where i wish to render 2 partial views in single layout.cshtml one below another. Each partial view belongs to separate controller. I processed it using 2 different approach, the first one which i wanted it to work didn't worked out. What i wanted it is to have partial view controller of categoris1 (see div-id below in layout.cshtml) and then below it display partial view of categoris2 div.
let's say this is my _Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - ABCD</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/bootstrap-theme.min.css" rel="stylesheet" />
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
@Html.ActionLink("TitleName", "Index", "Files", new { area = "" }, new { @class = "navbar-brand" })
</div>
</div>
</div>
<div class="row panel">
//My first view should be displayed here
<div id="categoris1" class="col-lg-3">
@Html.Action("DispatchedProducts", "DispatchedProducts")
</div>
//Second view should be displayed here just below first one
<div id="categoris2" class="col-lg-3">
@Html.Action("DisplayUnDispatchedProducts", "UnDispatchedProducts")
</div>
<div class="col-lg-8">
@RenderBody()
</div>
<hr />
<footer>
<p>© @DateTime.Now.Year - My MVC Application</p>
</footer>
</div>
</body>
</html>
This results in displaying of the categoris1 div controller view only when i run application (but not categoris2 partialview)and categoris2 div partial view is displayed at other url (when i write path to controller and view in browser) and what i wanted it is to display on same page just below categoris1 .
My next approach which worked was i just kept this categoris2 div (line below) in the partial view of 'DispatchedProducts
' at the bottom of DisplayDispatchedProducts.cshtml
and it displayed exactly what i expected.
<div id="categoris2" class="col-lg-3">
@Html.Action("DisplayUnDispatchedProducts", "UnDispatchedProducts")
</div>
Complete DisplayDispatchedProducts.cshtml something like this:
@model IList<ABC.Models.Product>
@{
ViewBag.Title = "First view";
}
<div class="col-md-10">
<h2>Dispatched Items</h2>
<div class="form-group">
<label class="col-md-2 control-label">Dispatched Items</label>
<table>
@foreach (var item in Model)
{
<tc>
<td>
<img [email protected] height="100" width="100" />
</td>
</tc>
}
<hr />
</table>
</div>
</div>
and DisplayDispatchedProducts controller has method like this (similar is other controller and partial view with same return types but different queries) :
public PartialViewResult DisplayDispatchedProducts()
{
productDispatched = uow.ProductsRepository.GetProductWithOrder().ToList();
return PartialView(productDispatched);
}
But i want to move with the first approach to have call to both partial views in just _Layout.cshtml
file like i mentioned above. Which i expected to display both in a row below each other. Is it feasible to happen ? How to make it work ?
Please let me know if any other info needed
Upvotes: 0
Views: 567
Reputation: 20
Have you tried calling Html.RenderPartial or Html.RenderAction?
There are other overrides for each of these methods depending on which version of the framework you are using.
Upvotes: 0