繁体   English   中英

MVC4 AllowHtml无法使用Dictionary <string, string>

[英]MVC4 AllowHtml not working with Dictionary<string, string>

我在C#MVC4项目中有这个课程:

public class SaveModel
{
    ....

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

一个控制器动作看起来像这样

public ActionResult SaveTemplate(SaveModel model)
{  
    ....
}

BodyHtml工作正常,但由于某种原因,AllowHtml不能在Dictionary上工作,我收到这样的错误:

A potentially dangerous Request.Form value was detected from 
the client (additionalTemplate[0].value="<tr>..."

有没有什么方法可以绕过它,除了通过在我的操作上放置[ValidateInput(false)]来禁用整个请求的验证?

[ValidateInput(false)]
public ActionResult SaveTemplate(SaveModel model)
{  
    ....
}

作为快速解决方法,您可以为键值集合创建自己的类型,其中包含两个属性。 Value属性可以标记为AllowHtml,如:

    public List<MyCustomItem> AdditionalTemplate { get; set; }  

布拉布拉

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM