3

Anyone know if it is possible to do the following:

public class User
{
    public Guid UserID { get; set; }

    [Required(ErrorMessage="A school selection is required.")]
    [Range(1, int.MaxValue, ErrorMessage="School not found.  Please contact support at <a href='mailto:[email protected]'>[email protected]</a>")]
    public int SchoolID { get; set; }

    // ... other properties
}

And not have @Html.ValidationMessageFor(model => model.SchoolID) encode the HTML? Maybe I need to make a custom helper extension for this? Is there an easier way? Any guidance would be appreciated. If a helper extension is the way to go, how could I access the validation rules in the helper extension method to make sure the html doesn't get encoded?

Thanks.

3 Answers 3

0

Depending on how often you needed to use this, you could use the HttpUtility.HtmlDecode method on the validation message.

@{HttpUtility.HtmlDecode(Html.ValidationMessageFor(x=>x.SchoolId).ToString)}

The more reusable, but more upfront time involved method would be to create your own html helper (maybe called HtmlValidationMessageFor()) that returns a non-html encoded string.

With that said, the return type for the ValidationMessageFor function is an MvcHtmlString which states that it is

Represents an HTML-encoded string that should not be encoded again.

But I could not find a solid reason as to why this should not be encoded again except that it may be a double encoded string.

Sign up to request clarification or add additional context in comments.

1 Comment

I've actually used HTML when using MvcHtmlString.Create() and it outputs to the view without encoding, which has worked fine so far. I assume that is the point since that's how the standard helper methods work by generating html and outputting it via a MvcHtmlString. Really, the question now is how to get to the Range attribute ErrorMessage text from within the helper extension method and have that recreated as a MvcHtmlString?
0

I did some more searching and came across the SO post below that addresses how to do what I am asking. I've added that answer here in case anyone comes across this in the future. I'm going to mark this as the answer as well since it is closest to the best answer I was looking for, but I'll let the moderators decide if this needs to be closed as a duplicate.

Render HTML tags inside of HTML.ValidationMessageFor in MVC3

Comments

0

I just needed a workaround for one error message. So a simple jquery detour;

 <script>
    $(document).ready(function() {
       $('.validation-summary-errors ul li').each(function(){
            if($(this).html() == "School not found"){
                $(this).html("School not found.  Please contact support at <a href='mailto:[email protected]'>[email protected]</a>");
            }
       });
    });

Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.