我有一个使用RazorViewEngine的网站构建,其中使用“_ViewStart.cshtml”将布局设置为“Shared / _Layout.cshtml”。然后,我有以下模块:
public class LogModule : NancyModule
{
public LogModule()
{
Get["/log"] = _ =>
{
var list = GetLog().ToPagedList(1, 5);
return View["Index", list];
};
Get["/log/{page:int}"] = _ =>
{
int pageNumber = _.page ?? 1;
var list = GetLog().ToPagedList(pageNumber, 5);
return View["_List", list];
};
}
}
以下观点:
Index.cshtml
@using Nancy.ViewEngines.Razor
@using PagedList
@inherits NancyRazorViewBase<IPagedList<LogModel>>
<h1>View Log</h1>
<div id='container'>
@Html.Partial("_List", Model)
</div>
_List.cshtml
@using Nancy.ViewEngines.Razor
@using PagedList
@inherits NancyRazorViewBase<IPagedList<LogModel>>
<table class="table table-hover table-condensed">
<thead>
<tr>
<th>Date</th>
<th>Message</th>
</tr>
</thead>
<tbody>
@foreach (var log in Model)
{
<tr class="@log.Class">
<td>@log.Date</td>
<td>@log.Message</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="2">
<div class="pagination" data-current="@Model.PageNumber" data-count="@Model.PageCount">
<ul class="list-unstyled list-inline">
<li><a href="javascript:void(0)" class="pager first">|<</a></li>
<li><a href="javascript:void(0)" class="pager prev"><</a></li>
<li class="active"><span>@Model.PageNumber / @Model.PageCount</span></li>
<li><a href="javascript:void(0)" class="pager next">></a></li>
<li><a href="javascript:void(0)" class="pager last">>|</a></li>
</ul>
</div>
</td>
</tr>
</tfoot>
</table>
最后,一些javascript代码管理对'/ log / {page:int}“动作的ajax请求,并用结果替换'container'div。可悲的是,这个结果包含一个完整的页面,包括_Layout.cshtml并打破了所有页面。
在MVC中,这是使用返回PartialView(viewName,Model)解决的,但我在NancyFx中找不到类似的东西。有什么我想念的吗?
答案 0 :(得分:2)
原来,解决方案非常简单。只需使用以下一行创建一个“空”布局文件:
@RenderBody()
然后在局部视图中使用它:
@{ Layout = "Shared/_EmptyLayout"; }