Is there a way to allow a user to submit html content while still enabling model validation?

蹲街弑〆低调 提交于 2019-12-11 02:13:40

问题


I need to allow users to submit a form value containing html in their text inputs. This is an internally-facing application so it's reasonably safe to do so. I have succesfully used the

[ValidateInput(false)] 

attribute on the method in question, but this inhibits all model validation for the method/view model in question, but I only want to allow html in one of the TextBoxes and do not necessarily wish to write my own guard clauses for every other piece of model validation in the same method/view model, when I could would prefer to continue using Data Annotations for all of the other properties in the view model. It's too bad I cannot apply the [ValidateInput(false)] to only a single property of my viewmodel. I would assume that I need to override mvc's default model validation, but I cannot find any documentation on how to do so. Every search yields results describing only how to write my own custom validation attributes, which isn't correct for the problem I'm trying to solve. Thanks!


回答1:


You would have to upgrade your aplication to ASP.NET MVC 3. There you have AllowHtmlAttribute, which you can use to disable input validation on property level. ASP.NET MVC 3 is backward compactible with ASP.NET MVC 2 so the upgrade should be easy.



来源:https://stackoverflow.com/questions/5640535/is-there-a-way-to-allow-a-user-to-submit-html-content-while-still-enabling-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!