I got a little lost with the creation of the MVC3 helper. I have a helper that simply creates a string with an expression that is passed as a parameter.
I want to use my htmlHelper as follows:
@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 has been removed):
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 parameter is @<text>...</text>not valid for HtmlHelper, because it is a “lambda expression” and cannot be converted to>
Can anyone help solve this problem? I just want to pass any type @<text></text>to my HtmlHelper, and it just compiles it and puts the MvcHtmlString into a formatted return.
DECISION:
, :
ASP.Net MVC3 -
@<text></text> Func<object, HtmlHelper>!