IPV*_*rde 6 c# lambda html-helper asp.net-mvc-3
创建一个MVC3助手我有点失落.我有我的助手,只需创建一个带有作为参数传递的表达式的行.
我想像我这样使用我的htmlHelper:
@Html.AddTableFormField(model => model.UserName, @<text>
@Html.EditorFor(m => m.UserName)<span class="warning">Use only letters</span>
</text>)
Run Code Online (Sandbox Code Playgroud)
这是我的HtmlHelper(删除了一些不相关的代码):
public static MvcHtmlString AddTableFormField<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> property, Expression<Func<TModel>> customFormat = null)
{
var metadata = ModelMetadata.FromLambdaExpression(property, htmlHelper.ViewData);
string displayName = metadata.DisplayName;
var propName = metadata.PropertyName;
if (string.IsNullOrWhiteSpace(displayName))
displayName = propName;
MvcHtmlString htmlCustomFormat = null;
if (customFormat != null)
{
var deleg = customFormat.Compile();
htmlCustomFormat = new MvcHtmlString(deleg().ToString());
}
else
htmlCustomFormat = htmlHelper.EditorFor(property);
return new MvcHtmlString(string.Format("<tr>"+
"<td class=\"form-label\">"+
"<label class=\"editor-label\" for=\"{0}\">{1}<label>"+
"</td>"+
"<td class=\"form-data\">"+
"{2}"+
"</td>"+
"</tr>",
propName, displayName, htmlCustomFormat));
}
Run Code Online (Sandbox Code Playgroud)
我甚至无法编译它,因为该@<text>...</text>参数对于HtmlHelper无效,因为它是一个'lambda表达式'并且无法转换为Expression>
有人可以帮忙解决吗?我只是希望将任何类型传递@<text></text>给我的HtmlHelper,它只是编译它并将它的MvcHtmlString放入格式化的返回中.
解:
我在这篇文章中找到了我想要的东西: ASP.Net MVC3 - 将剃刀标记作为参数传递
参数类型@<text></text>必须是Func<object, HtmlHelper>!
| 归档时间: |
|
| 查看次数: |
1969 次 |
| 最近记录: |