自定义强类型助手

发布于 2024-12-03 08:10:05 字数 897 浏览 5 评论 0原文

我使用自定义助手时遇到错误:

CS1593:委托“System.Action”不接受 1 个参数

这是视图代码:

@Html.BsLookUp(Model => Model.FieldId, Model.FieldDescription)

和助手:

 public static MvcHtmlString BsLookUp<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string initialText)
    {
        string fieldName= ExpressionHelper.GetExpressionText(expression);

        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);

        StringBuilder sb = new StringBuilder("");
        sb.AppendFormat("<input type='text' name='{0}' value='{1}'/>", campo, initialText);

        return MvcHtmlString.Create(sb.ToString());
    }

如果我直接在视图中传递字符串,则此工作有效:

@Html.BsLookUp(Model => Model.HandleMotivoglosa, "any text here..")

I´m getting errors using my custom helpers:

CS1593: Delegate 'System.Action' does not take 1 arguments

Here is the view code:

@Html.BsLookUp(Model => Model.FieldId, Model.FieldDescription)

And the helper:

 public static MvcHtmlString BsLookUp<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string initialText)
    {
        string fieldName= ExpressionHelper.GetExpressionText(expression);

        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);

        StringBuilder sb = new StringBuilder("");
        sb.AppendFormat("<input type='text' name='{0}' value='{1}'/>", campo, initialText);

        return MvcHtmlString.Create(sb.ToString());
    }

if I pass a string directly in my view this works:

@Html.BsLookUp(Model => Model.HandleMotivoglosa, "any text here..")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-12-10 08:10:05

您是否注意到:

@Html.BsLookUp(Model => Model.FieldId, Model.FieldDescription)
//             ^        ^              ^
//             |        |              |
//             |        |              +- here, Model is a variable already defined
//             |        |
//             +--------+- here, Model is (supposed to be) a new variable

该错误似乎很晦涩,但请尝试将代码更改为:

@Html.BsLookUp(m => m.FieldId, Model.FieldDescription)

Have you noticed:

@Html.BsLookUp(Model => Model.FieldId, Model.FieldDescription)
//             ^        ^              ^
//             |        |              |
//             |        |              +- here, Model is a variable already defined
//             |        |
//             +--------+- here, Model is (supposed to be) a new variable

The error seems quite obscure, but try changing your code to:

@Html.BsLookUp(m => m.FieldId, Model.FieldDescription)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文