如何获取部分视图以显示数据库中的数据,Html.Partial 在 MVC 5 中

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

在我的网站上,我有一个 <aside>,我想在其中列出头条新闻。

以下是我迄今为止尝试过的一些方法:

  1. 已使用 @RenderPage() 显示消息列表 - 失败
  2. 已使用 @Html.Partial("_TopNews", Model) - Error: Object reference not set to an instance of an object。 - 我还在页面顶部声明了 @model IEnumerable<Namespace.Models.TopNews>
  3. @Html.RenderPartial 尝试了同样的事情 - 结果相同。
  4. 我在 TopNews 控制器中创建了一个 ActionResult (_DisplayTopNews),在 TopNews 视图文件夹中创建了一个视图。我使用了 @Html.Partial("_DisplayTopNews", Model),但仍然没有运气。

这是上述以 <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>

头条新闻部分:

@model IEnumerable<Namespace.Models.TopNews>


<table class="table">

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Message)
        </td>
    </tr>
}

</table>

_DisplayTopNews 视图

@model IEnumerable<Namespace.Models.TopNews>


<ul class="list">

    @foreach (var item in Model)
    {
        <li>
            @Html.DisplayFor(modelItem => item.Message)
        </li>
    }

</ul>

TopNewsController ActionResult 方法 _DisplayTopNews():

private DbContext db = new DbContext();
public ActionResult _DisplayTopNews()
{
    return View(db.TopNews.ToList());
}

谁能指出我做错了什么,我该如何改正?

*我想补充一点,所有 CRUD 功能都有效。

所以,回答我自己的问题。我意识到的第一件事是我不希望我的部分内容与其他 TopNews 视图混在一起。所以我在 Home 控制器中创建了局部视图。

这是部分内容:

@model IEnumerable<Namespace.Models.TopNews>
@foreach (var item in Model)
{
    <blockquote class="left">
        <small>
            @Html.DisplayFor(modelItem => item.Message)
        </small>
    </blockquote>
}

除了在页面上的显示方式,其他都一样。

在我的 Home 控制器中,我添加了一个返回视图 _TopNews 和模型的 ActionResult。

这是 HomeController ActionResult:

private readonly DbContext db = new DbContext();
public ActionResult _TopNews()
{
    return PartialView("_TopNews", db.TopNews.ToList());
}

<aside> 部分将被渲染的地方,我没有使用 @Html.Partial@Html.RenderPartial,而是使用了 @Html.Action.

这里是 <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>

我希望这对其他人有帮助。