Getting the error here:
ValueProviderResult value = bindingContext.ValueProvider.GetValue("ConfirmationMessage");
How do I allow on a selection of values only? i.e.
[ValidateInput(false)]
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult value = bindingContext.ValueProvider.GetValue("ConfirmationMessage");
ValueProviderResult value2 = bindingContext.ValueProvider.GetValue("ConfirmationMessage2");
}
Here are the steps to encode at client level and decode it at server level:
Post the form using jquery submit method.
In jquery button click event method encode field that you want to post to server. Example:
In Controller Level access all form id value using
Sample example:
Controller level:
Client level:
In Document Ready function:
Expanding upon the answer from @D-W, in my Edit controller, in iterating over form values, I had to replace all instances of
Request.Params.AllKeys
withRequest.Unvalidated.Form.AllKeys
and all instances ofRequest[key]
withRequest.Unvalidated.Form[key]
.This was the only solution that worked for me.
You have a few options.
On the model add this attribute to each property that you need to allow HTML - best choice
On the controller action add this attribute to allow all HTML
Brute force in web.config - definitely not recommended
In the web.config file, within the tags, insert the httpRuntime element with the attribute requestValidationMode="2.0". Also add the validateRequest="false" attribute in the pages element.
More info: http://davidhayden.com/blog/dave/archive/2011/01/16/AllowHtmlAttributeASPNETMVC3.aspx
The above works for usages of the default modelbinder.
Custom ModelBinder
It appears that a call to bindingContext.ValueProvider.GetValue() in the code above always validates the data, regardless any attributes. Digging into the ASP.NET MVC sources reveals that the DefaultModelBinder first checks if request validation is required and then calls the bindingContext.UnvalidatedValueProvider.GetValue() method with a parameter that indicates if validation is required or not.
Unfortunately we can’t use any of the framework code because it’s sealed, private or whatever to protect ignorant devs from doing dangerous stuff, but it’s not too difficult to create a working custom model binder that respects the AllowHtml and ValidateInput attributes:
The other required piece is a way to retrieve an unvalidated value. In this example we use an extension method for the ModelBindingContext class:
More info on this at http://blogs.taiga.nl/martijn/2011/09/29/custom-model-binders-and-request-validation/
Try: