Search code examples
c#asp.net-mvc-5asp.net-mvc-partialview

How do I get a partial view to display with data from a database, Html.Partial in MVC 5


On my website, I have an <aside> that I would like to list top news in.

  • I created a database to store the top news articles and connected it to my project using Entity Framework. It's a simple database with an ID and Message property.
  • I created a partial view to render just the messages without the ability to edit them from the partial view. It's essentially a copy of the Index view generated by EF when it created the database using code-first.
  • I'm using @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:

  1. Used @RenderPage() to display the list of messages - failed
  2. Used @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.
  3. Tried the same thing with @Html.RenderPartial - same results.
  4. I created an ActionResult (_DisplayTopNews) in the TopNews Controller and a View in the TopNews view folder. I used @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.


Solution

  • 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.