自定义强类型帮助程序

时间:2011-09-06 15:03:34

标签: asp.net-mvc helper

我使用自定义帮助程序收到错误:

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..")

1 个答案:

答案 0 :(得分:0)

你注意到了吗?

@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)