AllowHtml attribute doesn't work

2019-04-12 16:44发布

问题:

I'm trying to implement Tiny MCE text editor to my Create page. I've used [AllowHtml] attribute to my Body property but it still doesn't work. My detail view for the blog still showing html tags.

This is my blog entity class.

public class Blog
{
    public int Id { get; set; }

    [Required]
    public string Title { get; set; }

    [AllowHtml]
    [Required]
    [DisplayName("Content")]
    public string Body { get; set; }

    [DisplayName("Created on")]
    [DataType(DataType.Date)]
    [Required]
    public DateTime Created { get; set; }

}

The pic below shows my create blog page (which shows that I have TinyMCE implemented property and working)

This pic below shows my detail page for a blog. The problem here is that it stills showing html tags even though I've allowed html to my Body property.

回答1:

AllowHtml attribute just helps with model binding , by not validating(white listing html tags) against html tags . It does not do any thing with UI.

To display the text value and not html encode it, you can use @Html.Raw(Model.property)