ValueTuple in Razor

898 views Asked by At

I using vs2017 with the latest update MVC 5.2.6 with Razor c# 7.3 .Net Framework 4.7.2

I have a situation where I'm trying to pass a tuple from a view to a partial view

@{var tuple = (FirstName: "Babe", LastName: "Ruth"); } @Html.Partial("_Name", tuple)

and here's the partial view receiving the data

@model (string FirstName, string LastName)

If I use the immediate window, I can see this

Model ("Babe", "Ruth") FirstName: "Babe" LastName: "Ruth" Raw View: ("Babe", "Ruth")

but if I try and access the parts, I get this

Model.FirstName null Model.LastName null

so when I try and use the values in the view, it fails.

I can pass a collection that shows the same way, but it will iterate and display the information.

my question is, what am I missing. the fact the immediate window shows it correctly but I can't get the individual values.

as you can see, it recognizes the "properties" in the tuple, but can't get the values.

I've tried both the System.Tuple 4.5 nugget package and the one in mscorlib.

is there some secret to using a tuple in a razor view that I'm missing?

1

There are 1 answers

2
Jigar Sangoi On

You can try to access model valuee using this Model.Item1 & Model.Item2.