MVC 3 RTM allowHtml doesn't work when using FormCollection

后端 未结 2 1519
独厮守ぢ
独厮守ぢ 2021-01-19 18:35

MVC 3 RTM. Have a model that has an attribute with AllowHtml. In my controller action, if the action has FormCollection as parameter, it throws the exception:



        
2条回答
  •  误落风尘
    2021-01-19 19:36

    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);
    }
    

提交回复
热议问题