Is it possible to use ValueTuple as model in View?

Viewed 4775

Is it possible to use value tuples as model type in views in ASP.NET Core MVC? I mean like this:

Controller:

public IActionResult Index()
{
    ...

    (int ImportsCount, int ExportsCount) importsExports = (imports.Count, exports.Count);
    return View(importsExports);
}

View:

@model (int ImportsCount, int ExportsCount)

However, using this setup, exception is raised when page is rendered. I am using .NET 4.6.2 with System.ValueTuple NuGet package installed.

enter image description here

4 Answers

Did some experimenting after a while and I got it working with the syntax from question's text. Ie. both these options are working now:

@model (string, string) 
@model (string x, string y)

That means there is no need to explicitly state ValueTuple keyword anymore.

My current set up is Visual Studio 15.8.0 and ASP.NET Core 2.1.1.

EDIT: Also works in Visual Studio 2019

Related