【问题标题】:how to insert partial view from another controller in asp.net mvc 5如何在asp.net mvc 5中从另一个控制器插入部分视图
【发布时间】:2015-07-08 03:27:48
【问题描述】:

我想将部分视图从 NewsController 插入 HomeController。
在我的 NewsController 中

public ActionResult LastNewsPatial()
        {
            var lstLastNews = db.Articles.Take(5).OrderByDescending(m => m.CreatedDate).ToList();
            return PartialView(lstLastNews);
        }

在 Views/News 文件夹中我创建 LastNewsPatial.cshtml

@model IEnumerable<mytest.com.Models.Article>
@foreach (var item in Model) {
    <div class="glidecontent">
        <img src="@item.ImageURL" style="float: left; margin-right:21px;" />
        <a href="#" class="title"><strong>@item.Title</strong></a><br /><br />
        @item.Content
    </div>
}

在 Views/Home/Index.cshtml 我插入一个 LastNewsPatial 视图

@Html.Partial("~/Views/News/LastNewsPatial.cshtml")

当我运行我的项目时,我收到了一个错误

Object reference not set to an instance of an object.

在行

@foreach(模型中的变量项)

LastNewsPatial.cshtml

我该如何解决?

【问题讨论】:

    标签: asp.net asp.net-mvc-5


    【解决方案1】:

    您的错误的原因是 Model 没有传递给视图......实际上甚至根本没有调用该操作。设置断点,您将确认这一点。

    我认为您应该在索引中调用 @Html.RenderAction 而不是 @Html.Partial

    而不是@Html.Partial("~/Views/News/LastNewsPatial.cshtml")

    使用

    @Html.RenderAction("LastNewsPatial","News")
    

    @Html.Action("LastNewsPatial","News")

    【讨论】:

    • 当我使用@Html.RenderAction("LastNewsPatial","News") 然后我收到错误Cannot implicitly convert type 'void' to 'object'
    • @TienKenji 你在哪里得到这个错误?指定了吗?
    • 当我使用 @Html.Action("LastNewsPatial","News") 时出现错误 Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
    • @TienKenji 你把这个@Html.Action 放在哪里?在索引视图中还是在哪里?
    • 是的,我将索引视图放在 Views/Home/Index.cshtml
    【解决方案2】:

    renderingview 时,您无需在视图名称中提供.cshtml。像这样只给出PartialView 的名称而不是.cshtml

    @Html.Partial("~/Views/Shared/LastNewsPatial")
    

    你也可以使用@Html.Action()来渲染你的视图

    @Html.Action("LastNewsPatial","News")
    

    【讨论】:

    • Additional information: The partial view '~/Views/News/HotNewsPartial' was not found or no view engine supports the searched locations. 再次调整@Html.Partial("~/Views/News/LastNewsPatial") 时收到错误
    • 是的,因为您的 path 不正确。请更正path
    • 我的正确路径是@Html.Partial("~/Views/News/LastNewsPatial")
    • @TienKenji 看看我的回答 .. 视图没有要渲染的模型 .. 因此出现错误。使用我指定的方法,你就会有你需要的。
    • 当我使用@Html.Action("LastNewsPatial","News") 时出现错误Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
    【解决方案3】:

    这对我有用:@Html.Partial("../Views/Shared/LastNewsPatial")

    .. 代替 ~

    【讨论】:

      【解决方案4】:

      这里的一些答案缺少问题 你用这个

      @Html.Partial("~/Views/News/LastNewsPatial.cshtml")
      

      这会创建一个没有模型的局部视图。所以为了让它工作,你需要通过模型。

          @Html.Partial("~/Views/News/LastNewsPatial.cshtml", your_model)
      

      您的问题与视图无关,您只是没有将对象传递给部分。如何做到这一点取决于你。你想要一个 Html.Partial 还是 Html.Action?根据您的需要。

      附言

      使用 RenderPartial 和 RederAction 它们是更好的做法,因为 Partial 和 Action 返回一个 HTML 字符串,而使用渲染允许 ASP.NET 直接写入响应流。

      【讨论】:

        猜你喜欢
        • 2016-01-24
        • 2023-03-12
        • 2016-02-10
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        • 2012-04-15
        • 2021-11-13
        • 1970-01-01
        相关资源
        最近更新 更多