Created
August 1, 2014 21:34
-
-
Save Dekryptid/1ea4e3e8c1b7723dfeff to your computer and use it in GitHub Desktop.
Bootstrap ASP.NET MVC Validation Summary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
namespace Nurvx.Mvc.Helpers | |
{ | |
public static class MvcHtmlHelper | |
{ | |
public static MvcHtmlString BootstrapValidationSummary(this HtmlHelper helper, string validationMessage = "", bool dismissible = false) | |
{ | |
// If the ModelState has no errors, return empty | |
if (helper.ViewData.ModelState.IsValid) | |
return MvcHtmlString.Empty; | |
// Create the validation summary div wrapper element | |
TagBuilder divValidationSummary = new TagBuilder("div"); | |
divValidationSummary.MergeAttribute("class", "validation-summary-errors alert alert-danger alert-dismissible"); | |
divValidationSummary.MergeAttribute("role", "alert"); | |
// If dismissible add the close button | |
if (dismissible) | |
{ | |
// Create the button element for closing | |
TagBuilder buttonClose = new TagBuilder("button"); | |
buttonClose.MergeAttribute("type", "button"); | |
buttonClose.MergeAttribute("class", "close"); | |
buttonClose.MergeAttribute("data-dismiss", "alert"); | |
// Create 'X' span | |
TagBuilder spanButtonCloseX = new TagBuilder("span"); | |
spanButtonCloseX.MergeAttribute("aria-hidden", "true"); | |
spanButtonCloseX.InnerHtml = "×"; | |
// Create 'Close' text span | |
TagBuilder spanButtonCloseText = new TagBuilder("span"); | |
spanButtonCloseText.MergeAttribute("class", "sr-only"); | |
spanButtonCloseText.InnerHtml = "Close"; | |
// Add both span elements to the button | |
buttonClose.InnerHtml = spanButtonCloseX.ToString(TagRenderMode.Normal) + spanButtonCloseText.ToString(TagRenderMode.Normal); | |
divValidationSummary.InnerHtml += buttonClose.ToString(TagRenderMode.Normal); | |
} | |
if (!String.IsNullOrWhiteSpace(validationMessage)) | |
{ | |
// If a validation message is specified, add it as bold text | |
TagBuilder strongValidationMessage = new TagBuilder("strong"); | |
strongValidationMessage.InnerHtml = validationMessage; | |
divValidationSummary.InnerHtml += strongValidationMessage.ToString(TagRenderMode.Normal); | |
} | |
// Create the validation messages "ul" element | |
TagBuilder ulValidationErrorMessages = new TagBuilder("ul"); | |
foreach (var key in helper.ViewData.ModelState.Keys) | |
{ | |
foreach (var err in helper.ViewData.ModelState[key].Errors) | |
{ | |
// Create and add validation message "li" element to the "ul" element | |
TagBuilder liValidationErrorMessage = new TagBuilder("li"); | |
liValidationErrorMessage.SetInnerText(helper.Encode(err.ErrorMessage)); | |
ulValidationErrorMessages.InnerHtml += liValidationErrorMessage.ToString(TagRenderMode.Normal); | |
} | |
} | |
// Add the ul element to the validation summary div element | |
divValidationSummary.InnerHtml += ulValidationErrorMessages.ToString(TagRenderMode.Normal); | |
return MvcHtmlString.Create(divValidationSummary.ToString(TagRenderMode.Normal)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment