bizzehdee
bizzehdee

Reputation: 20993

How do i use the AllowHtml attribute with the entity framework

How would i go about adding the [AllowHtml] attribute to an entity framework generated class without the attribute being overwritten the next time the code is generated?

I am looking to simply allow CKEditor to post information back to my MVC4 application using Razor without having to use [ValidateReuqest(false)] on my Content entity class.

Upvotes: 1

Views: 2865

Answers (2)

Edwin Rooks
Edwin Rooks

Reputation: 56

Add a new C# partial class file to the model folder with the same name as your entity class and apply the attribute there. Ensure that the namespaces for the partial classes match up, otherwise they are seen as different classes.

You can use the same partial class to set other attributes like [Display(Name="xxx")] for other properties.

Upvotes: 0

bizzehdee
bizzehdee

Reputation: 20993

You can use the [MetadataType] attribute to add metadata/attributes to your classes permanently without having to edit the original classes.

For the class Content create a new cs file in your project and replace the empty class with:

[MetadataType(typeof(ContentMetadata))]
public partial class Content
{

}

public class ContentMetadata
{
    [AllowHtml]
    public string ContentHtml { get; set; }
}

The partial class name must match the class name of the entity class exactly, and the attribute must match the definition of the attribute in the entity class exactly.

After a rebuild, this will now work as if you put the attribute within the entity class, but with the added bonus of not being overwritten every time.

Upvotes: 10

Related Questions