Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Razor throws CompilationFailedException when iterating over named Tuple #1070

Closed
MaxxDelusional opened this issue Mar 9, 2017 · 5 comments
Closed

Comments

@MaxxDelusional
Copy link

When trying to iterate over a collection of named Tuples, I receive a runtime error that the tuple does not contain the named member.

 public class MyClass
 {
        public static IEnumerable<(string name, int age)> GetPeople()
        {
            return new(string, int)[]  {
                    ("Dave", 32),
                    ("Mary", 29),
                };
        }
}

Razor

<div>
    @foreach (var person in MyClass.GetPeople())
    {
        <p>@person.name</p>
    }
</div>

At runtime, the view will throw the following exception

An unhandled exception has occurred while executing the request
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
/Views/Default/Test.cshtml(84,34): error CS1061: 'ValueTuple<string, int>' does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type 'ValueTuple<string, int>' could be found (are you missing a using directive or an assembly reference?)

If I change my class to return unnamed Tuples, and change the Razor code use Item1, and Item2 instead, it works perfectly.

I am able to iterate over the collection in my controller, but it will not work in a Razor view.

@NTaylorMullen
Copy link
Member

Mvc/Razor do not currently support C#7

@MaxxDelusional
Copy link
Author

Oh okay, it's confusing because Visual Studio has intellisense support for it.

@NTaylorMullen
Copy link
Member

Ya, sorry about that @MaxxDelusional. Tooling runs with a different Roslyn than we use at runtime 😄. Follow #1046 for updates on when C#7 will make its way to you.

@Eilon
Copy link
Member

Eilon commented Mar 14, 2017

Closing as dup of #1046.

@Eilon Eilon closed this as completed Mar 14, 2017
@rynowak
Copy link
Member

rynowak commented Sep 6, 2017

For anyone reaching this through stackoverflow - I want to confirm that the following will result in a parsing error in 2.0.0.

@model (string, int)

This will be addressed in 2.1.

Use of tuples in the body of Razor code is fine, we missed the @model and similar directives for 2.0.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants