我已经编写了一个扩展方法来自定义我的验证消息,如下所示:
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());
}
}
}
所以在我看来,我把
@using HtmlHelpers
以及
@Html.ValidationMessageFor(model => model.Name)
但是我得到了这个例外:
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>>)'
我是MVC的新手......有什么帮助?
谢谢!
答案 0 :(得分:3)
您的扩展方法与默认方法具有相同的名称和签名。这是不可能的,因为你不能在范围内同时拥有2。您必须为您的方法指定一个不同的名称,或更改参数的数量和/或类型以避免冲突。
例如:
public static MvcHtmlString CustomValidationMessageFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression
)
{
...
}
然后像这样使用它:
@Html.CustomValidationMessageFor(model => model.Name)