1

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?

CodeApe
  • 11
  • 1
  • Related : https://stackoverflow.com/a/44287670 - Seemingly using ValueTuples directly as Models was only supported around Core 3.1 – StuartLC Sep 11 '21 at 08:14

1 Answers1

2

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

Jigar Sangoi
  • 159
  • 6
  • I have already tried that. No such property as Item1. I tried casting it to valuetuple<>. That gets the Item1 property, but it always null. – CodeApe Nov 05 '18 at 15:51
  • Please refer this [link](https://blog.jetbrains.com/dotnet/2017/10/24/c-7-0-7-1-support-resharper-tuples/). You need valuetuple to get the value in same way that was asked in question. Otherwise declare tuple with Tuple.Create() – Jigar Sangoi Nov 06 '18 at 16:45