diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
index 13ae3bf9ba..e799eef1d7 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
@@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// Initializes a new instance of the class.
///
public HtmlHelper(
- [NotNull] IViewEngine viewEngine,
+ [NotNull] IViewEngine viewEngine,
[NotNull] IModelMetadataProvider metadataProvider,
[NotNull] IUrlHelper urlHelper,
[NotNull] AntiForgery antiForgeryInstance)
@@ -98,13 +98,13 @@ namespace Microsoft.AspNet.Mvc.Rendering
///
public HtmlString ActionLink(
- [NotNull] string linkText,
- string actionName,
- string controllerName,
- string protocol,
- string hostname,
- string fragment,
- object routeValues,
+ [NotNull] string linkText,
+ string actionName,
+ string controllerName,
+ string protocol,
+ string hostname,
+ string fragment,
+ object routeValues,
object htmlAttributes)
{
var url = _urlHelper.Action(actionName, controllerName, routeValues);
@@ -508,6 +508,46 @@ namespace Microsoft.AspNet.Mvc.Rendering
return divBuilder.ToHtmlString(TagRenderMode.Normal);
}
+ public HtmlString ValidationMessage(string name, string message, object htmlAttributes)
+ {
+ ModelState modelState;
+ ViewData.ModelState.TryGetValue(name, out modelState);
+
+ ModelErrorCollection errors = null;
+ if (modelState != null)
+ {
+ errors = modelState.Errors;
+ }
+
+ bool hasError = errors != null && errors.Any();
+ if (!hasError && !ViewContext.UnobtrusiveJavaScriptEnabled)
+ {
+ return null;
+ }
+ else
+ {
+ string error = null;
+ if (hasError)
+ {
+ error = message ?? errors.First().ErrorMessage;
+ }
+
+ var tagBuilder = new TagBuilder("span") { InnerHtml = Encode(error) };
+ tagBuilder.MergeAttributes(AnonymousObjectToHtmlAttributes(htmlAttributes));
+
+ if (ViewContext.UnobtrusiveJavaScriptEnabled)
+ {
+ bool replaceValidationMessageContents = string.IsNullOrEmpty(message);
+ tagBuilder.MergeAttribute("data-valmsg-for", name);
+ tagBuilder.MergeAttribute("data-valmsg-replace",
+ replaceValidationMessageContents.ToString().ToLowerInvariant());
+ }
+
+ tagBuilder.AddCssClass(hasError ? ValidationMessageCssClassName : ValidationMessageValidCssClassName);
+ return tagBuilder.ToHtmlString(TagRenderMode.Normal);
+ }
+ }
+
///
/// Returns the HTTP method that handles form input (GET or POST) as a string.
///
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperValidationExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperValidationExtensions.cs
index 54f0d8d503..cbef6ccb22 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperValidationExtensions.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperValidationExtensions.cs
@@ -56,39 +56,40 @@ namespace Microsoft.AspNet.Mvc.Rendering
htmlAttributes: htmlAttributes);
}
- private IHtmlString BuildValidationMessage(string name, string message, IDictionary htmlAttributes)
+ public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
+ string modelName)
{
- var modelState = ModelState[name];
- IEnumerable errors = null;
- if (modelState != null)
- {
- errors = modelState.Errors;
- }
- bool hasError = errors != null && errors.Any();
- if (!hasError && !UnobtrusiveJavaScriptEnabled)
- {
- // If unobtrusive validation is enabled, we need to generate an empty span with the "val-for" attribute"
- return null;
- }
- else
- {
- string error = null;
- if (hasError)
- {
- error = message ?? errors.First();
- }
+ return ValidationMessage(htmlHelper, modelName, message: null, htmlAttributes: null);
+ }
- TagBuilder tagBuilder = new TagBuilder("span") { InnerHtml = Encode(error) };
- tagBuilder.MergeAttributes(htmlAttributes);
- if (UnobtrusiveJavaScriptEnabled)
- {
- bool replaceValidationMessageContents = String.IsNullOrEmpty(message);
- tagBuilder.MergeAttribute("data-valmsg-for", name);
- tagBuilder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
- }
- tagBuilder.AddCssClass(hasError ? ValidationMessageCssClassName : ValidationMessageValidCssClassName);
- return tagBuilder.ToHtmlString(TagRenderMode.Normal);
- }
+ public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
+ string modelName, string message)
+ {
+ return ValidationMessage(htmlHelper, modelName, message, htmlAttributes: null);
+ }
+
+ public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
+ string modelName, object htmlAttributes)
+ {
+ return ValidationMessage(htmlHelper, modelName, message: null, htmlAttributes: htmlAttributes);
+ }
+
+ public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
+ string modelName, IDictionary htmlAttributes)
+ {
+ return ValidationMessage(htmlHelper, modelName, message: null, htmlAttributes: htmlAttributes);
+ }
+
+ public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
+ string modelName, string message, IDictionary htmlAttributes)
+ {
+ return htmlHelper.ValidationMessage(modelName, message, htmlAttributes);
+ }
+
+ public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
+ string modelName, string message, object htmlAttributes)
+ {
+ return htmlHelper.ValidationMessage(modelName, message, htmlAttributes);
}
}
}
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs
index 5eb8d91fe0..d8ecfdd115 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs
@@ -223,6 +223,16 @@ namespace Microsoft.AspNet.Mvc.Rendering
HtmlString TextBoxFor([NotNull] Expression> expression, string format,
IDictionary htmlAttributes);
+ ///
+ /// Returns the validation message if an error exists in the object.
+ ///
+ /// The name of the property that is being validated.
+ /// The message to be displayed if the specified field contains an error.
+ /// Dictionary that contains the HTML attributes which should
+ /// be applied on the element
+ ///
+ HtmlString ValidationMessage(string modelName, string message, object htmlAttributes);
+
///
/// Returns the model value for the given expression .
///