【问题标题】:MVC4 AllowHtml not working with Dictionary<string, string>MVC4 AllowHtml 不适用于 Dictionary<string, string>
【发布时间】:2013-07-31 19:27:59
【问题描述】:

我在 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)
{  
    ....
}

【问题讨论】:

  • 你没有展示你的视图是什么样的,但你可以使用@Html.Raw。这似乎不值得作为 answer 发布,但它会阻止视图 HTML 对您的字符串进行编码。
  • 我也遇到了这个问题,还没找到解决办法。

标签: c# asp.net-mvc-4 model-binding


【解决方案1】:

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

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

废话

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    相关资源
    最近更新 更多