Shawn Mclean
Shawn Mclean

Reputation: 57469

Model attributes to ignore validation of html entities

I want to input html in the database and also display it back as html. I wrote my view model like this:

public class TemplateVM
{
    [HiddenInput(DisplayValue = false)]
    public int TemplateId { get; set; }
    public string Name { get; set; }
    public string Content { get; set; }
}

the property Content should be able to accept html. How can I do this? Right now, it throws the error of:

A potentially dangerous Request.Form value was detected from the client (Content="<p>test</p>").

I'm aware of using this on the action, but I dont want it to apply to every property.:

[ValidateInput(false)]

Upvotes: 1

Views: 1097

Answers (2)

frennky
frennky

Reputation: 13934

Instead of using ValidateInput attribute on entire model, I suggest you use AllowHtml attribute on Content property:

public class TemplateVM
{
    [HiddenInput(DisplayValue = false)]
    public int TemplateId { get; set; }
    public string Name { get; set; }
    [AllowHtml]
    public string Content { get; set; }
}

This attribute is only applied for Content property, while other properties are still validated.

Upvotes: 4

Jakub Konecki
Jakub Konecki

Reputation: 46008

Put [ValidateInput(false)] on top of TemplateVM. It will apply to all properties.

Upvotes: 3

Related Questions