MVC3 - 通过换行来ModelState.AddModelError [英] MVC3 - passing NEW-LINE to ModelState.AddModelError
问题描述
在上后端发生了错误,MVC的控制器经结果,返回的消息 ModelState.AddModelError(,消息);
我想有一个信息在2号线 显示,所以我想提出一个 \\ r \\ n
结果
或 < BR />中。
之间
我用剃刀来显示使用 @ Html.ValidationSummary()消息;
结果
但是从查看HTML输出显示为 &放大器; LT; BR /&放大器; GT;
什么是传递信息的新行并获得最佳方式,除$ P $在HTML输出电平PTED成为一个真正的结果标签?
================================结果
控制器code:
ModelState.AddModelError(,一线474; BR />两线);
返回Request.IsAjaxRequest()? (ActionResult的)PartialView(视图名,模型)
查看(模型);
查看code:
@using(Ajax.BeginForm(指数,家,新AjaxOptions {UpdateTargetId =TV}))
{
@if(Html.ValidationSummary()!= NULL)
@ Html.Raw(Server.HtmlDe code(Html.ValidationSummary(真)的ToString())) ....
}
在ValidationSummary助手HTML EN codeS错误信息,这是由设计。这意味着你不能使用HTML标记,因为他们将连接codeD。所以,你可以写一个自定义帮助它不带code:
公共静态类ValidationExtensions
{
公共静态IHtmlString MyValidationSummary(此的HtmlHelper的HtmlHelper)
{
VAR formContextForClientValidation = htmlHelper.ViewContext.ClientValidationEnabled? htmlHelper.ViewContext.FormContext:空;
如果(htmlHelper.ViewData.ModelState.IsValid)
{
如果(formContextForClientValidation == NULL)
{
返回null;
}
如果(htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
{
返回null;
}
} StringBuilder的VAR =新的StringBuilder();
VAR ulBuilder =新TagBuilder(UL); ModelState中的ModelState;
如果(htmlHelper.ViewData.ModelState.TryGetValue(htmlHelper.ViewData.TemplateInfo.HtmlField preFIX,出来的ModelState))
{
的foreach(在modelState.Errors ModelError误差)
{
字符串userErrorMessageOrDefault = error.ErrorMessage;
如果(!string.IsNullOrEmpty(userErrorMessageOrDefault))
{
VAR liBuilder =新TagBuilder(「李先生」);
liBuilder.InnerHtml = userErrorMessageOrDefault;
stringBuilder.AppendLine(liBuilder.ToString(TagRenderMode.Normal));
}
}
} 如果(stringBuilder.Length == 0)
{
stringBuilder.AppendLine(<黎族风情= \\显示:无\\>< /李>);
}
ulBuilder.InnerHtml = stringBuilder.ToString(); TagBuilder divBuilder =新TagBuilder(分区);
divBuilder.AddCssClass(htmlHelper.ViewData.ModelState.IsValid HtmlHelper.ValidationSummaryValidCssClassName:HtmlHelper.ValidationSummaryCssClassName);
divBuilder.InnerHtml = ulBuilder.ToString(TagRenderMode.Normal);
如果(formContextForClientValidation!= NULL)
{
如果(!htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
{
divBuilder.GenerateId(的ValidationSummary);
formContextForClientValidation.ValidationSummaryId = divBuilder.Attributes [ID];
formContextForClientValidation.ReplaceValidationSummary = FALSE;
}
}
返回新HtmlString(divBuilder.ToString(TagRenderMode.Normal));
}
}
和则:
@ Html.MyValidationSummary()
这是我们自定义的助手以下行其中明确不带HTML code:
liBuilder.InnerHtml = userErrorMessageOrDefault;
在原来的助手,它看起来是这样的:
liBuilder.SetInnerText(userErrorMessageOrDefault);
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 <br/>
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()))
....
}
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);
这篇关于MVC3 - 通过换行来ModelState.AddModelError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!