在NancyFX中向我的Razor视图发送IEnumerable<string>类型的模型时,我得到了一个YSOD。 如果提供一个字符串作为模型,并且在视图中使用相关的@model语句,那么一切正常,因此它的工作@model 。
错误是
无法通过名称System.Collections.Generic.IEnumerable发现模型的CLR类型。 确保传递给视图的模型可分配给视图中声明的模型。
我错过了什么?
View.cshtml
@model System.Collections.Generic.IEnumerable<System.String> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1></h1> @foreach (var item in Model) { <h3>@item</h3> } </body> </html>模块
public class MyModule: NancyModule { public MyModule() { Get["/"] = parameters => View["View", this.GetModel()]; } private IEnumerable<string> GetModel() { return new[] { "one", "two" }; } }I'm getting a YSOD when sending a model of type IEnumerable<string> to my Razor view in NancyFX. Everything works well if supply a string as the model, with the relevant @model statement in the view, so its working.
The error is
Unable to discover CLR Type for model by the name of System.Collections.Generic.IEnumerable. Ensure that the model passed to the view is assignable to the model declared in the view.
What have I missed?
View.cshtml
@model System.Collections.Generic.IEnumerable<System.String> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1></h1> @foreach (var item in Model) { <h3>@item</h3> } </body> </html>The Module
public class MyModule: NancyModule { public MyModule() { Get["/"] = parameters => View["View", this.GetModel()]; } private IEnumerable<string> GetModel() { return new[] { "one", "two" }; } }最满意答案
问题似乎是Nancy不支持@model指令。 将@model为具有正确类型的@inherits可修复此问题:
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<My.ViewModels.WhateverViewModel>The problem seems to be the @model directive isn't supported in Nancy. Swapping the @model for an @inherits with the correct type fixes the issue:
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<My.ViewModels.WhateverViewModel>更多推荐
发布评论