我已经编写了自己的ValidationSummary,所以我可以更改ValidationSummary的输出,但是每当我提交表单时都没有出现警告消息。
然后我使用新的ValidationSummary在同一视图上添加了一个普通的ValidationSummary,并且在最初请求页面时输出完全相同。一旦按下提交按钮,正常的按钮就会工作并输出错误信息但我的没有。
它似乎没有回到服务器的帖子所以我错过了什么?
public static MvcHtmlString MyValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors, string message, IDictionary<string, object> htmlAttributes)
{
//<div class="validation-summary-errors" data-valmsg-summary="true"><ul><li>The User name field is required.</li><li>The Password field is required.</li></ul></div>
/*
* <div class="alert-wrapper error">
<div class="alert-text">
This is an error alert!
<a href="#" class="close">Close</a>
</div>
</div>
*
*/
if (htmlHelper == null)
{
throw new ArgumentNullException("htmlHelper");
}
FormContext formContext = htmlHelper.ViewContext.FormContext;
if (htmlHelper.ViewData.ModelState.IsValid)
{
if (formContext == null)
{ // No client side validation
return null;
}
// TODO: This isn't really about unobtrusive; can we fix up non-unobtrusive to get rid of this, too?
if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled && excludePropertyErrors)
{ // No client-side updates
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(errorText);
}
}
}
}
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);
TagBuilder innerDivBuilder = new TagBuilder("div");
innerDivBuilder.AddCssClass("alert-text");
TagBuilder closeBuilder = new TagBuilder("a");
closeBuilder.AddCssClass("close");
closeBuilder.Attributes.Add("href", "#");
divBuilder.InnerHtml = innerDivBuilder.InnerHtml + messageSpan + htmlSummary.ToString() + closeBuilder.InnerHtml;
if (formContext != null)
{
if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
{
if (!excludePropertyErrors)
{ // Only put errors in the validation summary if they're supposed to be included there
divBuilder.MergeAttribute("data-valmsg-summary", "true");
}
}
else
{
// client val summaries need an ID
divBuilder.GenerateId("validationSummary");
formContext.ValidationSummaryId = divBuilder.Attributes["id"];
formContext.ReplaceValidationSummary = !excludePropertyErrors;
}
}
return new MvcHtmlString(divBuilder.ToString(TagRenderMode.Normal));
//return divBuilder.ToMvcHtmlString(TagRenderMode.Normal);
}
答案 0 :(得分:0)
所以事实证明你必须在你的ValidationSummary中有<ul>
,然后javascript会将<li>
附加到unorderelist客户端,所以基本上你不能在ValidationSummary中更改HTML,如果你想要它工作客户端,然后没有更改验证JavaScript。