扩展 HTMLHelper 中的 ValidationSummary 以更改 HTML 输出
我已经编写了自己的 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);
}
I have written my own ValidationSummary so I could change the output of the ValidationSummary however whenever I submit the form no warning messages are appearing.
I then added a normal ValidationSummary on the same view with my new ValidationSummary and when the page is initially requested the output is exactly the same. As soon as the submit button is pressed the normal one works and outputs error messages but mine doesn't.
It doesnt seem to do a post back to the server so am I missing something?
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,事实证明,您必须在 ValidationSummary 中有一个
,然后 javascript 将
附加到 unorderelist 客户端,因此本质上您无法更改ValidationSummary 中的 HTML(如果您希望它在客户端工作而不更改验证 javascript)。
So it turns out that you have to have a
<ul>
in your ValidationSummary and the javascript then appends<li>
to the unorderelist client side so essentially you cannot change the HTML in ValidationSummary if you want it to work client side without then changing the validation javascript as well.