简体   繁体   中英

Dictionary TryGetValue not working in view

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

这可能与此问题有关 - Microsoft.Net.Compilers NuGet包的版本不够新,不足以支持out参数的内联声明。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM