@ Html.TextBoxFor throws System.FormatException when a localized string contains curly braces

@Html.TextBoxFor throws System.FormatException when a localized string contains curly braces

 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 gets the localized string using stringresourcekey . A localized string can contain characters such as braces, hashes, apostrophes, etc.

My cshtml uses MyProperty as follows.

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

When I run the asp.net mvc application in Visual Studio, this line System.FormatException . I know this is due to curly braces. But where and how can I avoid this? If I try to escape by replacing curly braces with double curly braces in GetLocalizedString , then Html will make double curly braces instead of single ones.

Update 1

I want that since I avoid the curly brace with double curly braces in the GetLocalizedString method (i.e. in C #), I want to display a single curly brace instead of double curly braces in HTML.

+5
source share
1 answer

Include HtmlEncode() in your view e.g.

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

Source: https://habr.com/ru/post/1240160/


All Articles