加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC 3 RTM allowHtml在使用FormCollection时不起

发布时间:2020-12-16 03:57:39 所属栏目:asp.Net 来源:网络整理
导读:MVC 3 RTM.拥有一个具有Allow Html属性的模型.在我的控制器操作中,如果操作将FormCollection作为参数,则会抛出异常: [HttpPost] public ActionResult Edit(FormCollection collection,int id) { var myEntity = _myRepo.Get(id); TryUpdateModel(myEntity);
MVC 3 RTM.拥有一个具有Allow Html属性的模型.在我的控制器操作中,如果操作将FormCollection作为参数,则会抛出异常:

[HttpPost]
 public ActionResult Edit(FormCollection collection,int id)
 {
   var myEntity = _myRepo.Get(id);

   TryUpdateModel(myEntity);

   return DoSave(myEntity);
 }

A potentially dangerous Request.Form
value was detected from the client

但是,如果我的控制器操作使用对象而不是FormCollection,则它不会抛出异常.

[HttpPost]
 public ActionResult Edit(MyEntity postedEntity,int id)
 {
   var myEntity = _myRepo.Get(id);

   TryUpdateModel(myEntity);

   return DoSave(myEntity);
 }

我已经设置好了

httpRuntime
requestValidationMode=”2.0″

使用FormCollection时为什么会失败?

解决方法

您不能将AllowHtml与FormCollection一起使用.您可以使用 [ValidateInput]属性,但显然这对所有值都禁用了验证:

[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(FormCollection collection,int id)
{
    var myEntity = _myRepo.Get(id);
    TryUpdateModel(objective);
    return DoSave(objective);
}

据说我会使用以下内容:

[HttpPost]
public ActionResult Edit(MyEntity entity)
{
    if (ModelState.IsValid)
    {
        _myRepo.Save(entity);
        return RedirectToAction("Success");
    }
    return View(entity);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读