【问题标题】:Access a Dictionary item in a view when using SSVE in Nancy在 Nancy 中使用 SSVE 时访问视图中的字典项
【发布时间】:2018-02-08 17:42:13
【问题描述】:

我正在将字典传递给我的视图。但我无法访问字典中的对象。

在 Nancy 中使用 SSVE 时,我们如何在视图中读取字典?这是我的代码:

帮助模块

public class HelpModule : NancyModule
{
    public HelpModule()
    {
        Get["/help/"] = _ => View["index"];
        Get["/help/faqs/"] = parameters => {
            return Negotiate
                .WithModel(new Dictionary<string, object>
                {
                    { "model" , new RatPack { FirstName = "Nancy " } },
                    { "info" , new RatPack { FirstName = "Info " } }
                })
                .WithView("faqs")
                .WithHeader("X-Custom", "SomeValue");
        };
    }
}

public class RatPack
{
    public string FirstName;
}

查看

@Each
    @Current
@EndEach

我得到的输出是

[模型,WebService.Website.Website.Modules.Help.RatPack] [信息,WebService.Website.Website.Modules.Help.RatPack]

我尝试了以下方法:

@Each
    @Current["info"]
@EndEach

@Each
    @Current.info
@EndEach

但我没有得到数据。

【问题讨论】:

  • 只是在黑暗中刺伤,但我假设在循环的上下文中,@CurrentKeyValuePair&lt;string,RatPack&gt;。因此,@Current.Value 会起作用吗?
  • 感谢您的建议,可行!

标签: c# .net dictionary nancy


【解决方案1】:

从@erdomke 的Comment,我们可以使用@Current.Value

如下:

@Each
    @Current.Key: @Current.Value.Firstname
@EndEach

这将给出输出:

型号:Nancy 信息:信息

【讨论】:

    猜你喜欢
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2019-06-20
    • 2020-05-11
    相关资源
    最近更新 更多