可以将名为 Tuple 的 C# 用作 MVC 页面模型类型吗? [英] Can a C# named Tuple be used as an MVC page model type?

查看:27
本文介绍了可以将名为 Tuple 的 C# 用作 MVC 页面模型类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 7 中,您可以命名元组:

In C# 7 you can have named tuples:

var foo = (Name: "Joe", Age: 42);

如果我使用以下方法将其传递给 MVC 模型:

If I pass this to a MVC model using:

return View(foo);

那么cshtml文件中应该用什么语法来声明模型呢?虽然这不起作用,但类似于...

Then what syntax should be used in the cshtml file to declare the model? Although this doesn't work, something like...

@model (string Name, int Age);

推荐答案

至于当前时间你不能且需要使用

As for current time you can't and need to use

@model Tuple<string, int>
//or
@model ValueTuple<string, int>

关于两个选项的区别:System.ValueTuple和System.Tuple有什么区别?

您可以在 GitHub 上关注:Razor 在迭代命名元组时抛出 CompilationFailedException (我知道它已被关闭,但名称更能代表当前案例)

You can follow on GitHub: Razor throws CompilationFailedException when iterating over named Tuple (I know it is closed as a dup but name is more indicative for current case)

这篇关于可以将名为 Tuple 的 C# 用作 MVC 页面模型类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆