i have wrote a extension method for customize my validation messages, like this:
namespace Helpers
{
public static class HtmlHelpers
{
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
var sb = new StringBuilder();
string modelName = ExpressionHelper.GetExpressionText(expression);
ModelState state = htmlHelper.ViewData.ModelState[modelName];
if (state != null)
if ((state.Errors != null) && (state.Errors.Count > 0))
{
sb.Append("<div class='error-left'></div>");
sb.Append("<div class='error-inner'>");
sb.Append(htmlHelper.ValidationMessageFor(expression).ToString());
sb.Append("</div>");
}
return MvcHtmlString.Create(sb.ToString());
}
}
}
So in my view, i put
@using HtmlHelpers
and also
@Html.ValidationMessageFor(model => model.Name)
But i’m getting this exception:
The call is ambiguous between the following methods or properties: 'ContinentalWeb.Helpers.HtmlHelpers.ValidationMessageFor<ContinentalWeb.Models.Maker,string>(System.Web.Mvc.HtmlHelper<Type>, System.Linq.Expressions.Expression<System.Func<Type,string>>)' and 'System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor<Type,string>(System.Web.Mvc.HtmlHelper<Type>, System.Linq.Expressions.Expression<System.Func<Type,string>>)'
I’m new in MVC… any help?
Thank you!
Your extension method has the same name and signature as the default one. This is not possible because you cannot have the 2 at the same time in scope. You will have to give a different name to your method or change the number and/or type of arguments to avoid the conflict.
For example:
and then use it like this: