如果不编写一些我不打算做的疯狂扩展方法,这可能是不可能的。我知道ValidationSummary()
是为了安全而编码的。 我只是好奇我是否遗漏了明显的东西......?
我的表单中有一个简单的<%= Html.ValidationSummary() %>
。对于某些情况,会抛出错误,如果我可以对错误的外观有更多的控制,那就太好了。
例如 - 当我导入CSV文件时,如果标题不是可接受的格式或包含特殊字符,我会将以下错误抛给ValidationSummary()
:
constraintValidatorContext.AddInvalid(invalidHeaders.Count() == 1
? string.Format("The following column header is invalid: {0}.", badHeaders)
: string.Format("There were multiple invalid column headers including: {0}.", badHeaders), "General");
如果我能够在列表中显示多个标题,那将会膨胀:
string.Format("There were multiple invalid column headers including: <li>{0}</li>", badHeaders)
这会显示HTML。 即使我可以在这里或那里投入NewLine,也会有所帮助(\ n \ r) ......我只是不知道什么?
答案 0 :(得分:1)
新行在HTML中无效,您需要<br />
标记。
您可以随时自行滚动以获得对输出的更多控制权。这是帮助程序的MVC2源代码。它基本上通过
来看public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors, string message, IDictionary<string, object> htmlAttributes) {
if (htmlHelper == null) {
throw new ArgumentNullException("htmlHelper");
}
FormContext formContext = htmlHelper.ViewContext.GetFormContextForClientValidation();
if (formContext == null && htmlHelper.ViewData.ModelState.IsValid) {
return null;
}
string messageSpan;
if (!String.IsNullOrEmpty(message)) {
TagBuilder spanTag = new TagBuilder("span");
spanTag.SetInnerText(message);
messageSpan = spanTag.ToString(TagRenderMode.Normal) + Environment.NewLine;
}
else {
messageSpan = null;
}
StringBuilder htmlSummary = new StringBuilder();
TagBuilder unorderedList = new TagBuilder("ul");
IEnumerable<ModelState> modelStates = null;
if (excludePropertyErrors) {
ModelState ms;
htmlHelper.ViewData.ModelState.TryGetValue(htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix, out ms);
if (ms != null) {
modelStates = new ModelState[] { ms };
}
}
else {
modelStates = htmlHelper.ViewData.ModelState.Values;
}
if (modelStates != null) {
foreach (ModelState modelState in modelStates) {
foreach (ModelError modelError in modelState.Errors) {
string errorText = GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, null /* modelState */);
if (!String.IsNullOrEmpty(errorText)) {
TagBuilder listItem = new TagBuilder("li");
listItem.SetInnerText(errorText);
htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal));
}
}
}
}
if (htmlSummary.Length == 0) {
htmlSummary.AppendLine(_hiddenListItem);
}
unorderedList.InnerHtml = htmlSummary.ToString();
TagBuilder divBuilder = new TagBuilder("div");
divBuilder.MergeAttributes(htmlAttributes);
divBuilder.AddCssClass((htmlHelper.ViewData.ModelState.IsValid) ? HtmlHelper.ValidationSummaryValidCssClassName : HtmlHelper.ValidationSummaryCssClassName);
divBuilder.InnerHtml = messageSpan + unorderedList.ToString(TagRenderMode.Normal);
if (formContext != null) {
// client val summaries need an ID
divBuilder.GenerateId("validationSummary");
formContext.ValidationSummaryId = divBuilder.Attributes["id"];
formContext.ReplaceValidationSummary = !excludePropertyErrors;
}
return divBuilder.ToMvcHtmlString(TagRenderMode.Normal);
}
ValidationSummaryValidCssClassName是“validation-summary-errors”,ValidationSummaryValidCssClassName是“validation-summary-valid”。