AllowHtml属性不起作用

我有一个具有此属性的模型:

[AllowHtml] [DisplayName("Widget for Table")] [StringLength(1000, ErrorMessage = "Maximum chars 1000")] [DataType(DataType.Html)] public object TableWidget { get; set; } 

这是控制器中的create方法:

  // // GET: /Admin/Table/Create public ActionResult Create(int id) { Season season = _seasonRepository.GetSeason(id); var table = new Table { SeasonId = season.SeasonId }; return View(table); } // // POST: /Admin/Table/Create [HttpPost] public ActionResult Create(Table a) { if (ModelState.IsValid) { _tableRepository.Add(a); _tableRepository.Save(); return RedirectToAction("Details", "Season", new { id = a.SeasonId }); } return View(); } 

最后这是我的观点:

 @model Stridh.Data.Models.Table @using (Html.BeginForm()) { @Html.ValidationSummary(true) 
Fields
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
@Html.LabelFor(model => model.TableURL)
@Html.EditorFor(model => model.TableURL) @Html.ValidationMessageFor(model => model.TableURL)
@Html.LabelFor(model => model.SortOrder)
@Html.EditorFor(model => model.SortOrder) @Html.ValidationMessageFor(model => model.SortOrder)
@Html.LabelFor(model => model.TableWidget)
@Html.EditorFor(model => model.TableWidget) @Html.ValidationMessageFor(model => model.TableWidget)

}

当我添加一个没有html的“正常”消息时,所有内容都保存好了,但保存时它说有一个潜在危险的Request.Form ……

另一个奇怪的事情是我让这个[AllowHtml]在另一个模型类中工作。 我不知道为什么这会让我烦恼。 需要你的帮助。 🙂

你使用AllowHtml应该工作。 确保您没有访问代码中任何其他位置的HttpRequest.Form集合(控制器,filter等),因为这将触发ASP.NET请求validation和您看到的错误。 如果您确实想要访问该变量,那么您应该通过以下代码访问它。

 using System.Web.Helpers; HttpRequestBase request = .. // the request object request.Unvalidated().Form; 

我得到了同样的问题,我在这篇文章的帮助下解决了这个问题 。

如果您使用的是.net 4.0,请确保将其添加到web.config中

  

标签内

我也有这个问题。 我无法获得标有[AllowHtml]的模型属性来实际允许HTML,而是遇到了您描述的相同错误。 我的解决方案最终是使用[ValidateInput(false)]属性标记接受已发布模型的Controller操作。

我有同样的问题。 我的模型类名为“GeneralContent”,其属性为“Content”。 在我的动作方法中,我使用了这样的属性:

公共ActionResult更新(GeneralContent内容)

当我将内容参数重命名为cnt时,一切运行良好。 我认为当模型类的某些属性与action方法中的参数同名时,MVC会感到困惑。