YSOD在NancyFX中传递复杂类型作为剃刀模型时(YSOD when passing complex type as razor model in NancyFX)

编程入门 行业动态 更新时间:2024-10-27 12:28:57
YSOD在NancyFX中传递复杂类型作为剃刀模型时(YSOD when passing complex type as razor model in NancyFX)

在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>

更多推荐

本文发布于:2023-08-08 00:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:剃刀   模型   类型   NancyFX   YSOD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!