C# AllowHtml属性不工作

C# AllowHtml属性不工作,c#,asp.net-mvc-3,model-binding,C#,Asp.net Mvc 3,Model Binding,我有一个具有此属性的模型: [AllowHtml] [DisplayName("Widget for Table")] [StringLength(1000, ErrorMessage = "Maximum chars 1000")] [DataType(DataType.Html)] public object TableWidget { get; set; } 以下是控制器中的创建方法: // // GET: /Admin/Tabl

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

     [AllowHtml]
     [DisplayName("Widget for Table")]
     [StringLength(1000, ErrorMessage = "Maximum chars 1000")]
     [DataType(DataType.Html)]
     public object TableWidget { get; set; }
以下是控制器中的创建方法:

  //
  // 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)
   <fieldset>
      <legend>Fields</legend>
      <div class="editor-label">
         @Html.LabelFor(model => model.Name)
      </div>
      <div class="editor-field">
         @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
      </div>
      <div class="editor-label">
         @Html.LabelFor(model => model.TableURL)
      </div>
      <div class="editor-field">
         @Html.EditorFor(model => model.TableURL) @Html.ValidationMessageFor(model => model.TableURL)
      </div>
      <div class="editor-label">
         @Html.LabelFor(model => model.SortOrder)
      </div>
      <div class="editor-field">
         @Html.EditorFor(model => model.SortOrder) @Html.ValidationMessageFor(model => model.SortOrder)
      </div>
      <div class="editor-label">
         @Html.LabelFor(model => model.TableWidget)
      </div>
      <div class="editor-field">
         @Html.EditorFor(model => model.TableWidget) @Html.ValidationMessageFor(model => model.TableWidget)
      </div>
      <div class="editor-label invisible">
         @Html.LabelFor(model => model.SeasonId)
      </div>
      <div class="editor-field invisible">
         @Html.EditorFor(model => model.SeasonId)
      </div>
      <p>
         <input type="submit" value="Create" />
      </p>
   </fieldset>
} 
@model Stridh.Data.Models.Table
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
领域
@LabelFor(model=>model.Name)
@Html.EditorFor(model=>model.Name)@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.TableURL)
@Html.EditorFor(model=>model.TableURL)@Html.ValidationMessageFor(model=>model.TableURL)
@LabelFor(model=>model.SortOrder)
@Html.EditorFor(model=>model.SortOrder)@Html.ValidationMessageFor(model=>model.SortOrder)
@LabelFor(model=>model.TableWidget)
@Html.EditorFor(model=>model.TableWidget)@Html.ValidationMessageFor(model=>model.TableWidget)
@LabelFor(model=>model.季候ID)
@EditorFor(model=>model.季候ID)

}
当我添加一条没有html的“普通”消息时,所有内容都保存得很好,但当保存它时,会显示一个潜在的危险请求。表单


另一件奇怪的事情是,我得到了这个[AllowHtml]在另一个模型类中工作。我不明白为什么这会给我带来麻烦。需要您的帮助。:-)

您使用
allowtml
的方式应该有效。确保您没有访问代码中任何其他位置(控制器、筛选器等)的
HttpRequest.Form
集合,因为这将触发ASP.NET请求验证和您看到的错误。如果您确实希望访问该变量,则应通过以下代码访问该变量

using System.Web.Helpers;

HttpRequestBase request = ..  // the request object
request.Unvalidated().Form;

我也有这个问题。我无法获取标记为
[AllowHtml]
的模型属性以实际允许HTML,而是遇到了与您描述的相同的错误。我的解决方案是用
[ValidateInput(false)]
属性标记接受发布模型的控制器操作。

我遇到了同样的问题,我在的帮助下解决了它

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

<httpRuntime requestValidationMode="2.0" />


标签中

我也遇到了同样的问题。我的模型类名为“GeneralContent”,具有属性“Content”。在我的操作方法中,我使用了如下属性:

公共行动结果更新(一般内容)


当我将content参数重命名为cnt时,一切正常。我认为,当模型类的某些属性与实际方法中的参数同名时,MVC会感到困惑。

答案是@marcind使我走上了正确的道路,但我的问题是我将FormCollection传递到了Controller方法中,因此更改此

public ActionResult Edit(MyClass myClass, FormCollection collection)
对此

public ActionResult Edit(MyClass myClass)
解决了这个问题

随后,我能够使用类似这样的代码毫无问题地访问heck-out表单集合

foreach (var key in Request.Form.AllKeys)
{
   ...
}

因此,导致问题的是传递表单集合参数,而不仅仅是访问表单集合。

也许这会有所帮助:您的web.config need@Fujiy此建议不一定正确。您不必设置requestValidationMode。还请包括您看到的异常的堆栈跟踪。这将有助于诊断问题。这一定是早期版本中的旧Asp.NETMVC错误。因为我在新安装的MVC中没有这些问题。但还是要感谢您的回答我一直在努力解决这个问题,在我的案例中,这很有帮助:)仅供参考,如果您使用自定义模型绑定,您可能会遇到类似的问题,您需要使用IUnvalidatedValueProvider来访问这些值:几乎有十年历史了,但仍然是一个出色的诊断!这不是一个好的做法。它不会对模型的任何属性进行任何验证。我们最近使用AllowHtml对停止工作的属性更新了服务器和功能。将此添加到web.config解决了此问题。但是请记住,通过AppResults:不要这样做,不安全。我可以确认这一点。在几个小时拉我的头发之后;将CreateTemplate(TextTemplate模板)更改为CreateTemplate(TextTemplate模型)解决了此问题。(TextTemplate有一个名为“Template”的属性)。