本文介绍了使用的FormCollection当MVC 3 RTM allowHtml不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 3 RTM。有一个与AllowHtml属性的模型。在我的控制器动作,如果行动的FormCollection作为参数,它会抛出异常:

  [HttpPost]
 公众的ActionResult编辑(的FormCollection收集,INT ID)
 {
   变种myEntity所= _myRepo.Get(ID);   TryUpdateModel(myEntity所);   返回DoSave就会(myEntity所);
 }

However if my controller action uses an object instead of FormCollection it doesn't throw the exception.

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

   TryUpdateModel(myEntity);

   return DoSave(myEntity);
 }

I've already setup

Why does it fail when using FormCollection?

解决方案

You can't use AllowHtml with FormCollection. You could use the [ValidateInput] attribute but obviously this disabled validation for all values:

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

This being said I would use the following:

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

这篇关于使用的FormCollection当MVC 3 RTM allowHtml不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:20