1

I have my view as something like the following,

class MyModel
{
  ...
  public IDictionary<string, string> Values {get; set;}
}

@model web.Models.MyModel

@{
   // This works
   if(Model.Values.ContainsKey("key"))
   {
        // ta daa
   }

   // If I include this part, I don't even get to reach the previous code
   // because the excpetion is thrown
    if(Model.Values.TryGetValue("key", out string myValue))
    {

    }
 }

The problem is if I include the TryGetValue part, I don't even get to execute any code in my view, because the exception is thrown before I land in to the view.

The error is as follows:

An error occurred during the compilation of a resource required to process 
this request. Please review the following specific error details and modify
your source code appropriately.

/Views/MyView.cshtml

Syntax error, ',' expected 

if(Model.Values.TryGetValue("key", out string myValue)) <-- I don't see any syntax error
rethabile
  • 3,029
  • 6
  • 34
  • 68
  • That code is okay. Probably somewhere else it is failing. What if you move `string myValue` out of the `if`? – Patrick Hofman Jun 20 '17 at 08:14
  • 7
    Could you try declaring the out parameter on a separate line, could be a razor issue with the c# version, see also https://stackoverflow.com/questions/42844341/using-c-sharp-7-features-inside-of-a-view-in-an-asp-net-mvc-core-project. – Matt Jun 20 '17 at 08:14
  • wow! you guys are geniuses. It works after moving `myValue` declaration outside. – rethabile Jun 20 '17 at 08:23
  • @matt: trying to close your comment as "not a comment but answer" ;) – Tim Schmelter Jun 20 '17 at 08:27

1 Answers1

0

This was likely related to this issue - the version of the Microsoft.Net.Compilers NuGet package was not recent enough to support inline declaration of an out parameter.

Steve Wilkes
  • 7,085
  • 3
  • 29
  • 32