对于ValidatorMessage MVC3扩展 [英] MVC3 Extension for ValidatorMessage
本文介绍了对于ValidatorMessage MVC3扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了一个扩展方法为定制我的验证消息,如:
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
和也
@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>>)'
我在MVC是新...任何帮助吗?
I'm new in MVC... any help?
感谢您!
推荐答案
您的扩展方法具有相同的名称和签名作为默认之一。因为你不能有2在范围内,同时这是不可能的。你将不得不给予不同的名称,你的方法或更改参数的数量和/或类型,以避免冲突。
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.
例如:
public static MvcHtmlString CustomValidationMessageFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression
)
{
...
}
,然后用它是这样的:
and then use it like this:
@Html.CustomValidationMessageFor(model => model.Name)
这篇关于对于ValidatorMessage MVC3扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文