MVC3 - 将NEW-LINE传递给ModelState.AddModelError

时间:2011-11-16 18:10:41

标签: asp.net-mvc-3

当后端发生错误时,MVC控制器通过
返回消息 ModelState.AddModelError("", "message");

我希望“消息”以2行显示,所以我想提出 "\r\n"
或者 "<br />"

我正在使用Razor使用@Html.ValidationSummary();来显示消息 但是,视图中的HTML输出显示为 &lt;br/&gt;

在消息上传递New-Lines并将其解释为HTML输出级别的真实标签的最佳方法是什么?

================================
控制器代码:

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

查看代码:

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

    ....
}

4 个答案:

答案 0 :(得分:16)

ValidationSummary帮助程序HTML编码错误消息,这是设计使然。这意味着您不能使用HTML标记,因为它们将被编码。所以你可以编写一个不编码的自定义助手:

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));
    }
}

然后:

@Html.MyValidationSummary()

我们的自定义帮助器中的以下行显式不进行HTML编码:

liBuilder.InnerHtml = userErrorMessageOrDefault;

在原始助手中,它看起来像这样:

liBuilder.SetInnerText(userErrorMessageOrDefault);

答案 1 :(得分:10)

尝试将验证摘要包装在Html.RawServer.HtmlDecode中,如下所示:

@Html.Raw(Server.HtmlDecode(Html.ValidationSummary().ToString()))

答案 2 :(得分:3)

控制器:

ModelState.AddModelError("MyError", "Line 1" + Environment.NewLine + "Line 2");

剃刀:

<span style="white-space: pre-line">@Html.ValidationSummary()</span>

答案 3 :(得分:1)

这是一个迟到的答案,但这是我谷歌的最佳结果,所以我的解决方案可以帮助某人。

我解决了这个问题,并为每条错误消息行添加了一个单独的行。显示得很漂亮。

控制器代码:

    public ActionResult Edit(EditModel model)
    {
            try
            {
                //... do update
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains(Environment.NewLine))
                {
                    var messages = ex.Message.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var message in messages)
                    {
                        this.ModelState.AddModelError(string.Empty, message);
                    }
                }
                else
                {
                    this.ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
        //...

        return this.View(model);
    }