Reputation: 875
I have the following code attempting to extend @Html.ValidationMessageFor with my own @Html.MyValidationMessageFor(model => model.myfield) and provide a custom error html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Linq.Expressions;
namespace Hml.BackEnd.Helpers
{
public static class HtmlHelpers
{
public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
string myContent = "<div>" + htmlHelper.ValidationMessageFor(expression).ToString() + "</div>";
return MvcHtmlString.Create(myContent);
}
}
}
I get the following compilation message:
'System.Web.Mvc.HtmlHelper < Model > does not contain a definition for ValidationMessageFor and no extension method ValidationMessageFor accepting a first argument of type 'System.Web.Mvc.HtmlHelper < TModel > could be found are you missing a using or assembly reference.
I can't understand since System.Web.Mvc is in the project references and in the using.
Upvotes: 4
Views: 3262
Reputation: 10756
ValidationExtensions.ValidationMessageFor is part of the System.Web.Mvc.Html namespace. You need to reference ValidationExtensions
either explicitly, using the full namespace, or include System.Web.Mvc.Html
within your using
directives.
Upvotes: 6