什么时候变量需要@,什么时候不需要?

When does a variable need an @ and when does it not?

我正在尝试增强 VS2013 中标准 MVC 模板附带的默认 Shared/Error.cshtml。模型是 System.Web.Mvc.HandleErrorInfo,我添加了以下内容:

@foreach (var key in Model.Exception.Data.Keys)
  <li>@key : @Model.Exception.Data[key]</li>

它在@Model.Exception.Data[key]中的key中给出错误:The name 'key' does not exist in the current context.

为什么以及如何从字典数据中获取每个值?

试试这个:

    @foreach (var key in Model.Exception.Data.Keys)
    {
      <li>@key : @Model.Exception.Data[key]</li>
    }

razor 需要“{”才能知道代码在 foreach 循环中