【问题标题】:YSOD when passing complex type as razor model in NancyFX在 NancyFX 中将复杂类型作为剃刀模型传递时的 YSOD
【发布时间】:2012-10-03 12:42:05
【问题描述】:

在将IEnumerable<string> 类型的模型发送到我在 NancyFX 中的 Razor 视图时,我收到了 YSOD。如果提供一个字符串作为模型,一切都很好,视图中有相关的@model 语句,所以 its 工作。

错误是

无法发现名为 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" };
    }
}

【问题讨论】:

    标签: razor nancy


    【解决方案1】:

    问题似乎是 Nancy 不支持 @model 指令。将@model 换成具有正确类型的@inherits 可以解决问题:

    @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<My.ViewModels.WhateverViewModel>
    

    【讨论】:

      【解决方案2】:

      除了Greg B's answer@model 仍然是 RazorEngine 中为 Nancy 保留的术语,尽管这在 Nancy Razor View Engine 页面上并不清楚。

      因此,您不能使用名称 model 声明变量并使用 @model.Property 引用它;视图引擎仍会尝试将其绑定到模型,即使这实际上不起作用 (Razor View Engine line 354),您也会收到相同的错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多