首页 文章

当本地化字符串包含大括号字符时,@ Html.TextBoxFor抛出System.FormatException

提问于
浏览
5

当本地化字符串包含大括号字符时, @Html.TextBoxFor 抛出 System.FormatException

public class MyModel
{
  [Display(ResourceType = typeof(MyModelResourceProvider), Name="MyProperty")]
  public string MyProperty { get; set; } 
  ...
}

public class MyModelResourceProvider
{
  public static string MyProperty
  {
    return GetLocalizedString("stringresourcekey");
  }
}

GetLocalizedString 使用 stringresourcekey 获取本地化字符串 . 本地化字符串可以包含大括号,哈希,撇号等字符 .

我的cshtml使用MyProperty如下 .

@Html.TextBoxFor(model => model.MyProperty, new { autocomplete = "off" })

当我在Visual Studio中运行我的asp.net mvc应用程序时,上面的行抛出 System.FormatException . 我知道这是由于大括号字符而发生的 . 但是我在哪里以及如何逃脱呢?如果我试图通过在 GetLocalizedString 中用双花括号替换花括号来逃避,则Html呈现双花括号而不是单花括号 .

Update 1

我想要的是,因为我在 GetLocalizedString 方法中使用双花括号(即在C#中)转义花括号,我想在HTML中显示单花括号而不是双花括号 .

1 回答

  • 0

    在您的视图中包含 HtmlEncode()

    @Html.TextBoxFor(model => Server.HtmlEncode(model.MyProperty))
    

相关问题