Search code examples
asp.net-mvcrazorhtml-helperasp.net-mvc-5.2

LabelFor incorrect when using AllowHtml


I'm using MVC 5.2.2.0, .net 4.5.1 and I'm seeing some odd behavior. I have a model like so:

public class Program
{
    // .... Other Properties 

    [Display(Name = "Courses Description")]
    [RichText]
    [AllowHtml]
    public string CoursesDescription { get; set; }

    // .... Other Properties
}

RichText is a custom attribute:

[AttributeUsage(AttributeTargets.Property)]
public class RichText : Attribute
{
    public RichText() { }
}

All it is used for is to tell the T4 template to add the wysi data-editor attribute to the TextAreaFor html helper.

My view has the following:

 <div class="form-group">
     @Html.LabelFor(model => model.CoursesDescription, htmlAttributes: new { @class = "control-label col-sm-3" })
     <div class="col-sm-6">
         @Html.TextAreaFor(model => model.CoursesDescription, 10, 20, new { @class = "form-control", data_editor = "wysi" })
         @Html.ValidationMessageFor(model => model.CoursesDescription, "", new { @class = "text-danger" })
     </div>
 </div>

With the AllowHtml attribute, the View renders like this:

Incorrect Label

However, if I modify the model by removing the AllowHtml attribute, the view renders correctly.

public class Program
{
    // .... Other Properties 

    [Display(Name = "Courses Description")]
    [RichText]
    public string CoursesDescription { get; set; }

    // .... Other Properties
}

Correct Rendering

It is also worth pointing out that removing the RichText attribute doesn't change anything, the View still ignores the Display attribute.

Any ideas why AllowHtml is interfering with LabelFor?


Solution

  • Digging deeper into this, I noticed that my MVC project was using MVC 5.2.2.0, but I had inadvertently installed MVC 5.2.3.0 for my models project.

    Downgrading the models project to 5.2.2.0 fixed the problem.