title: What is the easiest way to get the property value from a passed lambda expression in an extension method for HtmlHelper?
body: <p>I am writing a dirty little extension method for HtmlHelper so that I can say something like HtmlHelper.WysiwygFor(lambda) and display the CKEditor.</p>
  
  <p>I have this working currently but it seems a bit more cumbersome than I would prefer.  I am hoping that there is a more straight forward way of doing this.</p>
  
  <p>Here is what I have so far.</p>
  
  <pre><code>public static MvcHtmlString WysiwygFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression)
  {
      return MvcHtmlString.Create(string.Concat("&lt;textarea class=\"ckeditor\" cols=\"80\" id=\"",
                                          expression.MemberName(), "\" name=\"editor1\" rows=\"10\"&gt;", 
                                          GetValue(helper, expression),
                                          "&lt;/textarea&gt;"));
  }
  
  private static string GetValue&lt;TModel, TProperty&gt;(HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression)
  {
      MemberExpression body = (MemberExpression)expression.Body;
      string propertyName = body.Member.Name;
      TModel model = helper.ViewData.Model;
      string value = typeof(TModel).GetProperty(propertyName).GetValue(model, null).ToString();
      return value;
  }
  
  private static string MemberName&lt;T, V&gt;(this Expression&lt;Func&lt;T, V&gt;&gt; expression)
  {
      var memberExpression = expression.Body as MemberExpression;
      if (memberExpression == null)
              throw new InvalidOperationException("Expression must be a member expression");
  
      return memberExpression.Member.Name;
  }
  </code></pre>
  
  <p>Thanks!</p>