MVC4 AllowHtml not working with Dictionary

1

I have a class

public class TemplateViewModel
{
    [AllowHtml]
    public string Template { get; set; }

    [AllowHtml]
    public Dictionary<string, string> LocalizedContents { get; set; }
}

When I input html code for Template, it's fine. When I input html code for LocalizedContents, it prompt out the error

System.Web.HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (LocalizedContents[en-us]="...ate:
FB: <a href="www.faceboo...").
   at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
   at System.Web.HttpValueCollection.EnsureKeyValidated(String key)
   at System.Web.HttpValueCollection.GetValues(String name)
   at System.Web.Mvc.NameValueCollectionValueProvider.ValueProviderResultPlaceholder.GetResultFromCollection(String key, NameValueCollection collection, CultureInfo culture)
   at System.Web.Mvc.NameValueCollectionValueProvider.GetValue(String key, Boolean skipValidation)
   at System.Web.Mvc.ValueProviderCollection.GetValue(String key, Boolean skipValidation)
   at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
   at System.Web.Mvc.DefaultModelBinder.CreateEntryForModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type valueType, IModelBinder valueBinder, String modelName, Object modelKey)
   at System.Web.Mvc.DefaultModelBinder.UpdateDictionary(ControllerContext controllerContext, ModelBindingContext bindingContext, Type keyType, Type valueType)
   at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
   at System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
   at System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
   at System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext)
   at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model)
   at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState)

Any solution for the [AllowHtml] to be use for Dictionary?

I do not prefer to use the [ValidateInput(false)] as it will have security concern

I also do not want to include <httpRuntime requestValidationMode="2.0" /> in my web config

asp.net-mvc-4
data-annotations
asp.net-mvc-viewmodel
asked on Stack Overflow Jul 30, 2018 by Jay Chuah

1 Answer

0

The alternative way to achieve this is to create a class for the datatype and assign the [AllowHtml] for the property of the class.

public class LocalizedContents
{
    [AllowHtml]
    public string Key { get; set; }

    [AllowHtml]
    public string Value { get; set; }
}

Then put the class as datatype for LocalizedContents property

public class TemplateViewModel
{
    public string Template { get; set; }

    public List<LocalizedContents> LocalizedContents { get; set; }
}
answered on Stack Overflow Oct 9, 2019 by Jay Chuah

User contributions licensed under CC BY-SA 3.0