0

在我的_Layout.cshtml文件中,我想调用如下内容:

    <div id="content">
        <div id="left-wrapper" class="box">
            @Html.Action("FreeThisWeek", "Products")
            @RenderBody()
        </div>
    </div>

这是我的 ProductsController 文件:

[ChildActionOnly]
public ActionResult FreeThisWeek()
{
    //Some code that fetches the data and builds the model.
    var model = BuildFreeProducts();
    return View(model);
}

如果我尝试运行此代码,我会得到一个,StackOverflowException因为 Action 返回 View(),它要求布局,它运行返回 View() 的 Action,依此类推。

可以理解,但是我该如何完成这个正确的代码呢?

我在哪里编写将这个数据模型与我编写的 HTML 复合的视图?

4

2 回答 2

2

尝试返回PartialView("yourview",model)。确保您返回的视图没有将此页面用作布局。@{Layout=null}您可以通过在要返回的视图顶部使用来指定。

于 2012-04-09T19:43:32.333 回答
1

您将在FreeThisWeekAction 内返回 View 并在再次使用 _Layout 的 View 内返回。所以它变成递归的。

转到您的FreeThisWeek视图并将布局设置为空

@{
   Layout=null;
}
于 2012-04-09T19:50:58.997 回答