开发者

Is there a property that Html.ValidationSummary set?

I want to know if Html.ValidationSummary is present in code. The tag div class="validation-summary-valid" is not present in html if Html.ValidationSummary has a parameter of true. i.e. Html.ValidationSummary(true), the result, there's no reliable way to know in jquery if Html.ValidationSummary is in code.

Is there a property that says so? The presence or absence of Html.ValidationSummary

[EDIT: clarification]

Putting Html.ValidationSummary with a parameter of true:

@using (Html.BeginForm()) {

    @Html.ValidationSummary(true)   

    <b>Hello</b>

,and not putting any Html.ValidationSummary:

@using (Html.BeginForm()) {

    <b>Hello</b>

,both code yields the same HTML(View Page Source):

    <b>Hello</b>

That being so, there's no reliable way from jQuery to know if Html.ValidationSumm开发者_运维知识库ary is present or not in the code


It is happening because ValidationSummary helper does not return empty <div class="validation-summary-errors"/> if there is no errors (when ViewData.ModelState.IsValid returns true). In such case you need to have custom ValidationSummary which fixing that:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace System.Web.Mvc
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString CustomValidationSummary(this HtmlHelper helper, string validationMessage = null)
        {

            StringBuilder summary = new StringBuilder();

            summary.Append("<div class=\"validation-summary-errors\">");

            if (helper.ViewData.ModelState.IsValid)
            {
                summary.Append("</div>");

                return new MvcHtmlString(summary.ToString());
            }

            else if (!string.IsNullOrEmpty(validationMessage))
            {
                summary.Append("<ul>");
                summary.AppendFormat("<li>{0}</li>", validationMessage);

                summary.Append("</ul>");
                summary.Append("</div>");

                return new MvcHtmlString(summary.ToString());
            }


            summary.Append("<ul>");

            foreach (KeyValuePair<string, ModelState> state in helper.ViewData.ModelState)
            {
                foreach (ModelError error in state.Value.Errors)
                {
                    summary.AppendFormat("<li>{0}</li>", HttpUtility.HtmlEncode(error.ErrorMessage));
                }

            }

            summary.Append("</ul>");
            summary.Append("</div>");

            return new MvcHtmlString(summary.ToString());
        }
    }
}

This custom ValidationSummary helper will generate empty validation-summary-errors div even if there is was no errors, other words <div class="validation-summary-errors"/> would be present in your html every time, like you want.


If you're just talking about jQuery, can you not just do:

if ($('.validation-summary-valid').size() > 0)
{
  // Do Something
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜