Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MVC Razor Html.ValidationSummary show message as HTML

I want to show the validation summary as HTML format, for example.

Code behind (controller):

ModelState.AddModelError("", "Account not confirmed<br />Click <a href='#'>here</a> to resend the email confirmation");

Razor View:

@Html.ValidationSummary(true, "", new { @class = "text-danger" });

An the rendered HTML is like:

Account not confirmed<br />Click <a href='#'>here</a> to resend the email confirmation

I mean, the html message is shown as it, but I want it to show with HTML tags.

like image 325
Edgwin Avatar asked Sep 18 '25 21:09

Edgwin


1 Answers

You can create your own ValidationSummary, Create a partial and named it _MyValidationSummary.cshtml

@model ModelStateDictionary
@if(!Model.IsValid)
{
    <div class="validation-summary-errors">
        <p>
            "Account not confirmed<br />
            Click <a href='#'>here</a> to resend the email confirmation"
        </p>
        <ul>
            //Errors
        </ul>
    </div>
}

Refer to it:

@Html.Partial("_MyValidationSummary", ViewData.ModelState);
like image 57
AliD32 Avatar answered Sep 20 '25 10:09

AliD32