我试图在Nancy中将模型中的某些信息显示给我的视图。
public class Fixtures
{
public int Id { get; set; }
public Users HomeUser { get; set; }
public Users AwayUser { get; set; }
}
Get["/fixtures"] = _ =>
{
var model = new List<Fixtures>();
model.Add(new Fixtures() { Id = 1, HomeUser = new Users() { id = 1, Name = "Paddy" }, AwayUser = new Users() { id = 2, Name = "Dave" } });
model.Add(new Fixtures() { Id = 2, HomeUser = new Users() { id = 3, Name = "Scott" }, AwayUser = new Users() { id = 4, Name = "Chris" } });
return View["Fixtures", model];
};
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<System.Collections.Generic.List<Fifa.Leaderboard.Data.ViewModel.Fixtures>>
@{
Layout = "_Layout.cshtml";
}
<div id="fixtures">
@foreach (var fixture in Model)
{'
<p>@fixture.HomeUser</p>
<p>VS</p>
<p>@fixture.AwayUser</p>
}
</div>
任何人都知道我为什么会收到此错误?
答案 0 :(得分:0)
在原始问题的评论中提到了问题。
Nancy没有像ASP.NET MVC这样的共享视图文件夹的概念。
执行此操作的常规方法是将_Layout.cshtml
放在/Views
文件夹的根目录中。
如果您想避免视图位于根文件夹中或避免指定完整路径,您可以将查看位置添加到列表中,如下所示:
public class CustomConventionsBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
{
return string.Concat("views/shared/", viewName);
});
}
}
可在此处找到文档:
https://github.com/NancyFx/Nancy/wiki/View-location-conventions#defining-custom-conventions