I don't like the lines in my code, as the refactor fails. A good way is to use Linq expressions. If you get the model as ViewData, you can use the following statement:
<%= ShowDropDownBox(viewData => viewData.Name); %> ... public static string ShowDropDownList<T>(this HtmlHelper html, Expression<Action<T>> property) { var body = action.Body as MethodCallExpression; if (body == null) throw new InvalidOperationException("Expression must be a method call."); if (body.Object != action.Parameters[0]) throw new InvalidOperationException("Method call must target lambda argument."); string propertyName = body.Method.Name; string typeName = typeof(T).Name;
I know that the original solution works faster, but I think it is much cleaner.
Hope this helps!
source share