Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:48:39+00:00 2026-05-26T21:48:39+00:00

When an error occurs on the back-end, the MVC controller returns a message via

  • 0

When an error occurs on the back-end, the MVC controller returns a message via the
ModelState.AddModelError("", "message");

I would like to have that ‘message’ display in 2 lines, so I would like to put a "\r\n"
or a "<br />" in between.

I’m using Razor to display the Message using @Html.ValidationSummary();
But the HTML Output from the View displays that as &lt;br/&gt;

What is the best way to pass New-Lines on a message and get it interpreted into a real
tag at the HTML output level?

================================
Controller code:

ModelState.AddModelError("", "Line one <br /> Line two.");
return Request.IsAjaxRequest() ? (ActionResult) PartialView("ViewName", model) 
            : View(model);

View code:

@using (Ajax.BeginForm("Index", "Home", new AjaxOptions { UpdateTargetId = "tv" })) 
{
    @if (Html.ValidationSummary() != null)
        @Html.Raw(Server.HtmlDecode(Html.ValidationSummary(true).ToString()))   

    ....
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:48:39+00:00Added an answer on May 26, 2026 at 9:48 pm

    The ValidationSummary helper HTML encodes error messages and this is by design. It means that you cannot use HTML tags as they will be encoded. So you could write a custom helper which doesn’t encode:

    public static class ValidationExtensions
    {
        public static IHtmlString MyValidationSummary(this HtmlHelper htmlHelper)
        {
            var formContextForClientValidation = htmlHelper.ViewContext.ClientValidationEnabled ? htmlHelper.ViewContext.FormContext : null;
            if (htmlHelper.ViewData.ModelState.IsValid)
            {
                if (formContextForClientValidation == null)
                {
                    return null;
                }
                if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
                {
                    return null;
                }
            }
    
            var stringBuilder = new StringBuilder();
            var ulBuilder = new TagBuilder("ul");
    
            ModelState modelState;
            if (htmlHelper.ViewData.ModelState.TryGetValue(htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix, out modelState))
            {
                foreach (ModelError error in modelState.Errors)
                {
                    string userErrorMessageOrDefault = error.ErrorMessage;
                    if (!string.IsNullOrEmpty(userErrorMessageOrDefault))
                    {
                        var liBuilder = new TagBuilder("li");
                        liBuilder.InnerHtml = userErrorMessageOrDefault;
                        stringBuilder.AppendLine(liBuilder.ToString(TagRenderMode.Normal));
                    }
                }
            }
    
            if (stringBuilder.Length == 0)
            {
                stringBuilder.AppendLine("<li style=\"display:none\"></li>");
            }
            ulBuilder.InnerHtml = stringBuilder.ToString();
    
            TagBuilder divBuilder = new TagBuilder("div");
            divBuilder.AddCssClass(htmlHelper.ViewData.ModelState.IsValid ? HtmlHelper.ValidationSummaryValidCssClassName : HtmlHelper.ValidationSummaryCssClassName);
            divBuilder.InnerHtml = ulBuilder.ToString(TagRenderMode.Normal);
            if (formContextForClientValidation != null)
            {
                if (!htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
                {
                    divBuilder.GenerateId("validationSummary");
                    formContextForClientValidation.ValidationSummaryId = divBuilder.Attributes["id"];
                    formContextForClientValidation.ReplaceValidationSummary = false;
                }
            }
            return new HtmlString(divBuilder.ToString(TagRenderMode.Normal));
        }
    }
    

    and then:

    @Html.MyValidationSummary()
    

    It is the following line in our custom helper which explicitly doesn’t HTML encode:

    liBuilder.InnerHtml = userErrorMessageOrDefault;
    

    In the original helper it looks like this:

    liBuilder.SetInnerText(userErrorMessageOrDefault);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ironically i would like some feed back on a Stack level too deep error!
I want to make sure that if any error occurs during the database processing
What is the best workflow taken when connection error occurs. Let say we have
i have used impersonation in this application. whenever this error occurs i required to
I'm constructing a JFileFilter on a JFileChooser, however an error occurs where I have
Preface: I have a MS Access 2010 front end running against a MySQL back
I have the following classes. An error occurs during the for loop in the
I've got an MVC web application that uses SQL Server 2008 as a back
The error occurs when I try to do this friend std::ostream& operator<<(std::ostream& os, const
This is the error occurs in logcat,can anyone tell what's the meaning? private int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.