[英]MVC3 how to use @<text></text> as html helper parameter
I'm a little lost with the creation of an MVC3 Helper. 创建一个MVC3助手我有点失落。 I have my helper that just create a row with an expression that is passed as parameter. 我有我的助手,只需创建一个带有作为参数传递的表达式的行。
I want to use my htmlHelper like this: 我想像我这样使用我的htmlHelper:
@Html.AddTableFormField(model => model.UserName, @<text>
@Html.EditorFor(m => m.UserName)<span class="warning">Use only letters</span>
</text>)
This is my HtmlHelper (some irrelevant code was removed): 这是我的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));
}
I can't even compile it, because the @<text>...</text>
parameter is invalid for the HtmlHelper because it is an 'lambda expression' and cannot be converted into Expression> 我甚至无法编译它,因为@<text>...</text>
参数对于HtmlHelper无效,因为它是'lambda表达式'并且无法转换为Expression>
Anyone can help to solve it? 有人可以帮忙解决吗? I just want to pass any kind of @<text></text>
to my HtmlHelper, and it just compile it and put it's MvcHtmlString into the formated return. 我只想将任何类型的@<text></text>
传递给我的HtmlHelper,它只是编译它并将它的MvcHtmlString放入格式化的返回中。
SOLUTION: 解:
I found What I was looking for in this post: ASP.Net MVC3 - Pass razor markup as a parameter 我在这篇文章中找到了我想要的东西: ASP.Net MVC3 - 将剃刀标记作为参数传递
The parameter type for the @<text></text>
must be an Func<object, HtmlHelper>
! @<text></text>
的参数类型必须是Func<object, HtmlHelper>
!
I found What I was looking for in this post: ASP.Net MVC3 - Pass razor markup as a parameter 我在这篇文章中找到了我想要的东西: ASP.Net MVC3 - 将剃刀标记作为参数传递
The parameter type for the @<text></text>
must be an Func<object, HelperResult>
! @<text></text>
的参数类型必须是Func<object, HelperResult>
!
<text>...</text> or @:
是一个Razor语法(不是C#)来避免使用html元素,所以我的用法是使用string作为参数而不是@<text>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.