在我的_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(),它要求Layout,它运行返回View()的Action,依此类推。
可以理解,但我如何实现正确代码呢?
我在哪里编写使用我编写的HTML复合此数据模型的视图?
答案 0 :(得分:2)
尝试返回PartialView("yourview",model)
。确保您要返回的视图不将此页面用作布局。您可以通过在要返回的视图顶部使用@{Layout=null}
来指定。
答案 1 :(得分:1)
您将在FreeThisWeek
操作内以及您正在使用_Layout的视图内返回视图。所以它变得递归。
转到FreeThisWeek
视图并将布局设置为空
@{
Layout=null;
}