0
我已經編寫了自己的ValidationSummary,因此無論何時提交表單時都不會顯示警告消息,我可以更改ValidationSummary的輸出。在HTMLHelper中擴展ValidationSummary以更改HTML輸出
然後,我用新的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);
}