On my website, I have an <aside>
that I would like to list top news in.
@RenderPage()
to render the <aside>
. Inside of the <aside>
, I'm using @RenderPage()
to display some other information.Here are some of the things I've tried so far:
@RenderPage()
to display the list of messages - failed@Html.Partial("_TopNews", Model)
- Error: Object reference not set to an instance of an object
. - I also have the @model IEnumerable<Namespace.Models.TopNews>
declared at the top of the page.@Html.RenderPartial
- same results.@Html.Partial("_DisplayTopNews", Model)
, but with no luck still.Here is the code for the aforementioned items starting with the <aside>
:
@model IEnumerable<Namespace.Models.TopNews>
<aside class="sidebar sidebar-primary block-content-area col-md-4 panel-sides">
<section class="roundBox">
<div class="wrap">
<h4 class="block-title">Next Cycle Begins:</h4>
<div class="margin-top-lg text-left statement margin-sides-xl" aria-label="News and Announcements">
<div class="statement margin-sides pad-sides">
<h3>@RenderPage("~/Views/Shared/_CycleStateDate.cshtml")</h3>
</div>
</div>
</div>
</section>
<section class="sidebar-block sidebar_nav_menu roundBox">
<div class="wrap">
<h4 class="block-title"><strong>TOP NEWS</strong></h4>
<div class="margin-top-lg text-left statement margin-sides-xl" aria-label="News and Announcements">
<div class="statement margin-sides pad-sides">
@Html.Partial("_DisplayTopNews", Model)
</div>
</div>
</div>
</section>
</aside>
TopNews Partial:
@model IEnumerable<Namespace.Models.TopNews>
<table class="table">
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Message)
</td>
</tr>
}
</table>
_DisplayTopNews view
@model IEnumerable<Namespace.Models.TopNews>
<ul class="list">
@foreach (var item in Model)
{
<li>
@Html.DisplayFor(modelItem => item.Message)
</li>
}
</ul>
TopNewsController ActionResult Method _DisplayTopNews():
private DbContext db = new DbContext();
public ActionResult _DisplayTopNews()
{
return View(db.TopNews.ToList());
}
Can anyone please point out what I've done wrong and how I can correct it please?
*As a quick note I would like to add, all CRUD functionality works.
So, to answer my own question. The first thing I realized was that I didn't want my partial mixed in with the other TopNews views. So I created the partial view in my Home controller.
Here's the partial:
@model IEnumerable<Namespace.Models.TopNews>
@foreach (var item in Model)
{
<blockquote class="left">
<small>
@Html.DisplayFor(modelItem => item.Message)
</small>
</blockquote>
}
Besides how it will be displayed on the page, everything is the same.
In my Home controller I added an ActionResult that returned the view _TopNews and the model.
Here is the HomeController ActionResult:
private readonly DbContext db = new DbContext();
public ActionResult _TopNews()
{
return PartialView("_TopNews", db.TopNews.ToList());
}
In the <aside>
where the partial will be rendered, instead of using @Html.Partial
or @Html.RenderPartial
, I used @Html.Action
.
Here is the <aside>
:
@model IEnumerable<Namespace.Models.TopNews>
<aside class="sidebar sidebar-primary block-content-area col-md-4 panel-sides">
<section class="sidebar-block sidebar_nav_menu" aria-label="Top News sidebar">
<div class="panel panel-default">
<!--SIDEBAR HEADER-->
<div class="panel-heading">
<h3 class="Agenda">Top News</h3>
</div>
<div class="panel-body">
@Html.Action("_TopNews", "Home")
</div>
</div>
</section>
</aside>
I hope this helps someone else.