I am trying to generalize a complex control that is used on my site quite often, but with different fields. The functionality in the control is always the same, it is only the base fields that change.
To achieve a way to display different fields, I am trying to create an HTMLHelper extension that accepts Expression<Func<TModel,TProperty>> as a parameter that will contain the class properties needed to be displayed in the control. For example:
View:
@model Project.Core.Page @Html.MyHelper(p => new { p.Author.Name, p.Author.Location, p.Author.Age });
This is an extension I am having problems with - how can I TextBoxFor() over the provided parameters in lambda to provide each TextBoxFor() or even manually create an input element and fill it with the value and name parameter of the lambda?
Extension in psuedo:
public static MvcHtmlString MyHelper<TModel,TProperty>( this HtmlHelper<TModel> helper, Expression<Func<TModel,TProperty>> expression) { foreach (var parameter in expression.???) {
I feel like looking at it for too long, and I also feel that there is an easier way, I donโt notice it.
Any help is appreciated. If you need more information, or I missed something important, let me know.
Rory mccrossan
source share