Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post data from html editor to controller action in MVC

I am using jquery.cleditor.js plugin for html editor in my mvc application and it is working fine. Now I want to post the editor value to controller action but here I am getting the following exception:

A potentially dangerous Request.Form value was detected from the client 
    (NewContentPage.PageContents="<STRONG>dafs </STRON..."). 

If someone has idea then, please guide me to do this.

like image 618
munish Avatar asked Mar 17 '11 07:03

munish


1 Answers

If you add ValidateInput attribute with false it will allow you to submit HTML. Just be aware that you are turning of validation for all of the properties in the viewModel

[HttpPost, ValidateInput(false)]
public ActionResult DoStuff(MyViewModel viewModel)
{
    //...
}

If you are using .NET 4 you'll also have to set <httpRuntime requestValidationMode="2.0" /> in your web.config file.


If you are using MVC 3 you don't have to add the ValidateInput attribute to the controller action you can add AllowHtml attribute to the property in the viewModel.

public class MyViewModel
{
    public string prop1 { get; set; }

    [AllowHtml]
    public string prop2 { get; set; }
}

This allows HTML for prop2 but the rest of the MyViewModel will be validated.

like image 55
Bjarki Heiðar Avatar answered Nov 03 '22 04:11

Bjarki Heiðar