字典TryGetValue在视图中不起作用

时间:2017-06-20 08:10:18

标签: c# asp.net-core asp.net-core-mvc

我的观点如下,

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))
    {

    }
 }

问题是如果我包含TryGetValue部分,我甚至不能在我的视图中执行任何代码,因为在我登陆视图之前会抛出异常。

错误如下:

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

1 个答案:

答案 0 :(得分:0)

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