是否可以在View中使用ValueTuple作为模型?

Jan*_*las 6 c# asp.net-core-mvc valuetuple

是否可以在ASP.NET Core MVC中的视图中使用值元组作为模型类型?我的意思是这样的:

控制器:

public IActionResult Index()
{
    ...

    (int ImportsCount, int ExportsCount) importsExports = (imports.Count, exports.Count);
    return View(importsExports);
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model (int ImportsCount, int ExportsCount)
Run Code Online (Sandbox Code Playgroud)

但是,使用此设置时,呈现页面时会引发异常.我正在使用安装了System.ValueTuple NuGet包的.NET 4.6.2.

在此输入图像描述

SO *_*ood 9

通过一些测试,我发现了以下内容:

难道工作(生成视图编译错误的hundres):

@model (string, string) 
@model (string x, string y)
Run Code Online (Sandbox Code Playgroud)

是否工作:

@model ValueTuple<string, string>
@{ var ConvertedModel = ((string x, string y)Model);

<h1>@Model.Item1 | @ConvertedModel.x </h1>
<h1>@Model.Item2 | @ConvertedModel.y </h1>
Run Code Online (Sandbox Code Playgroud)

编辑:

通过查看GitHub问题(这里),似乎在Design Time Razor支持C#7,但不支持编译/运行时.

  • 朗姆酒时间总是最好的时间:-) (8认同)