Geeks With Blogs
Doug.Instance Improving the world one post at a time

While you can do most common tasks by simply passing expressions through to existing HtmlHelper extension methods, you may stumble on something where you want to evaluate the lambda expression on your own.  While this may seem daunting at first, the MS folks have made it pretty simple.  All you have to do is this:

ModelMetadata modelMetaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

This assumes that htmlHelper and epxression are parameters of your extension method which would look something like this:

public static MvcHtmlString MyNewExtensionMethod<T, TEnum>(this HtmlHelper<T> htmlHelper, Expression<Func<T, TEnum>> expression)

Then you can get the result of your expression with the following:

var result = modelMetaData.Model;

Note that this gives you the resulting value of the expression and NOT the instance of your model.

Posted on Friday, March 25, 2011 10:23 AM MVC | Back to top


Comments on this post: Key to Strongly-Typed HtmlHelper Extensions in ASP.Net MVC2

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Doug Lampe | Powered by: GeeksWithBlogs.net