自定义强类型的辅助
问题描述:
I'm使用我的自定义助手收到错误:自定义强类型的辅助
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..")
答
你有没有注意到:
@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)
+0
嗨,我测试了这种方式,并得到相同的错误。 –
是'Model.FieldDescription'一个字符串? – CodesInChaos
是的。这很奇怪,不是吗? –
你的问题有点不完整。您没有发布模型的相关部分。这种辅助方法有重载吗?而且这个观点也不存在。 – CodesInChaos