views:

52

answers:

2

How can I allow a user to input HTML into a particular field using ASP.net MVC.

I have a long form with many fields that get mapped to this complex object in the controller.

I would like to make one field (the description) allow HTML which I will preform my own sanitation on at a later point.

A: 

You can add [ValidateInput(false)] attribute to your controller action.

http://stephenwalther.com/blog/archive/2009/02/20/tip-48-ndash-disable-request-validation.aspx

I thought there was a way to easily disable it globally via web.config...

http://weblogs.asp.net/imranbaloch/archive/2010/06/08/handling-validateinputattribute-globally.aspx

dotjoe
+4  A: 

Add the following attribute the action (post) in the controller that you want to allow HTML for:

[ValidateInput(false)] 

Edit: As per Charlino comments:

In your web.config set the validation mode used. See MSDN:

<httpRuntime requestValidationMode="2.0" />
Kelsey
If you are using .NET 4 you'll also have to set `<httpRuntime requestValidationMode="2.0" />` in your web.config file.
Charlino
@Charlino good point... I will amend my answer to include that and some more information.
Kelsey
but wont that preclude me from being able to use validation on the rest of my Model?I have a large model for this particular page not to mention all of the other models edited by this controllerI was hoping to rely heavily on the ModelState and declarative Validation through attributes
Rabbi
@Rabbi it will remove all request validation for this action. It will not affect your model validation but all other fields will need to be checked and sanatized as well. You can't do it on a field by field level because the validation is happening at the point when the request is received which is way before the model is validated.
Kelsey
Is there really no solution for .NET 4 that doesn't include downgrading the request validation mode?
bzlm
@bzlm check out the MSDN link I posted... it doesn't need to be there but it is an option depending on the behaviour that they are after.
Kelsey