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

sur*_*aik 5 c# string asp.net-mvc

@Html.TextBoxForSystem.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");
  }
}
Run Code Online (Sandbox Code Playgroud)

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

我的cshtml使用MyProperty如下.

@Html.TextBoxFor(model => model.MyProperty, new { autocomplete = "off" })
Run Code Online (Sandbox Code Playgroud)

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

更新1

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

Rah*_*hul 0

包括HtmlEncode()在您的视图中,例如

@Html.TextBoxFor(model => Server.HtmlEncode(model.MyProperty))
Run Code Online (Sandbox Code Playgroud)