I am trying to write a helper in Razor that looks like the following:
@helper DoSomething(Expression> expr) where T : clas
if your main problem is to get name attribute value for binding using lambda expression seems like the @Html.TextBoxFor(x => x.MyPoperty)
, and if your component having very complex html tags and should be implemented on razor helper, then why don't just create an extension method of HtmlHelper
to resolve the binding name:
namespace System.Web.Mvc
{
public static class MyHelpers
{
public static string GetNameForBinding
(this HtmlHelper model,
Expression> property)
{
return ExpressionHelper.GetExpressionText(property);
}
}
}
your razor helper should be like usual:
@helper MyComponent(string name)
{
}
then here you can use it
@TheHelper.MyComponent(Html.GetNameForBinding(x => x.MyProperty))