使用表达式参数在自定义 html 帮助器中调用 asp.net mvc Html 帮助器
我正在尝试在 asp.net mvc 框架内编写 html 帮助器扩展。
public static MvcHtmlString PlatformNumericTextBoxFor<TModel>(this HtmlHelper instance, TModel model, Expression<Func<TModel,double>> selector)
where TModel : TableServiceEntity
{
var viewModel = new PlatformNumericTextBox();
var func = selector.Compile();
MemberExpression memExpession = (MemberExpression)selector.Body;
string name = memExpession.Member.Name;
var message = instance.ValidationMessageFor<TModel, double>(selector);
viewModel.name = name;
viewModel.value = func(model);
viewModel.validationMessage = String.Empty;
var result = instance.Partial(typeof(PlatformNumericTextBox).Name, viewModel);
return result;
}
该行
var message = instance.ValidationMessageFor<TModel, double>(selector);
有语法错误。但我不明白。错误是:Fehler 2“System.Web.Mvc.HtmlHelper”定义了“ValidationMessageFor”,并且是最佳方法“System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)" weist einige ungültige Argumente auf. C:\Projects\WorkstreamPlatform\WorkstreamPlatform_WebRole\Extensions\PlatformHtmlHelpersExtensions.cs 97 27 WorkstreamPlatform_WebRole
所以根据消息,该参数无效。但该方法实际上是这样声明的:
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression);
所以实际上它应该有效。
I am trying to write an html helper extension within the asp.net mvc framework.
public static MvcHtmlString PlatformNumericTextBoxFor<TModel>(this HtmlHelper instance, TModel model, Expression<Func<TModel,double>> selector)
where TModel : TableServiceEntity
{
var viewModel = new PlatformNumericTextBox();
var func = selector.Compile();
MemberExpression memExpession = (MemberExpression)selector.Body;
string name = memExpession.Member.Name;
var message = instance.ValidationMessageFor<TModel, double>(selector);
viewModel.name = name;
viewModel.value = func(model);
viewModel.validationMessage = String.Empty;
var result = instance.Partial(typeof(PlatformNumericTextBox).Name, viewModel);
return result;
}
The line
var message = instance.ValidationMessageFor<TModel, double>(selector);
has a syntax error. But I do not understand it. The error is: Fehler 2 "System.Web.Mvc.HtmlHelper" enthält keine Definition für "ValidationMessageFor", und die Überladung der optimalen Erweiterungsmethode "System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)" weist einige ungültige Argumente auf. C:\Projects\WorkstreamPlatform\WorkstreamPlatform_WebRole\Extensions\PlatformHtmlHelpersExtensions.cs 97 27 WorkstreamPlatform_WebRole
So according to the message, the parameter is invalid. But the method is actually declared like this:
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression);
So actually it should work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将方法声明更改为:
注意通用
this HtmlHelper
。此外,第二个参数也不是必需的,因为您可以从强类型帮助器中检索模型:Change your method declaration to:
Notice the generic
this HtmlHelper<TModel>
. Also the second argument is not necessary as you can retrieve the model from the strongly typed helper: