2

I am trying to build an html helper that would have access to modelmetadata. I need both versions of helper to work: from string expression and from lambda expression: Example:

public static MvcHtmlString MyLabel(this HtmlHelper html, string htmlFieldName)
{
    return LabelHelper(html, htmlFieldName);
}

public static MvcHtmlString MyLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    return LabelHelper(html, ExpressionHelper.GetExpressionText(expression));
}

private MvcHtmlString LabelHelper(HtmlHelper html, string htmlFieldName)
{
     ModelMetadata m = ModelMetadata.FromStringExpression(htmlFieldName);
     // the rest of the code...
}

The problem with the code above is that it will not work for complex types. For example, if my Model looked like this:

public class MyViewModel
{
    public int Id { get; set; }
    public Company Company { get; set; }
}

public class Company
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

My html helper will fail to read metadata for the following:

@Html.MyLabel("Company.Name")

I could make it work for the helper that takes an expression because ModelMetadata.FromLambdaExpression(...) actually works fine with complex objects, but that is not enough for me.

Any suggestions are appreciated.

Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79

1 Answers1

4

In a word, it will not be possible to use only the FromStringExpression(...) method. Internally the ModelMetadata.FromStringExpression(...) will try to get the ViewDataInfo for the nested property - "Name" in your case. If the View is a stongly-typed, but the Model is null then the
ViewData.GetViewDataInfo will return null. In this case it will loop only the ModelMetadata.Properties and will not be able to find the nested property. If the Model is not null, then the method will return the correct ModelMetadata, because of the correct ViewDataInfo. The ModelMetadata.FromLamdaExpression(...) on the other has enough information about the container and the type of the property and that is why it works with complex objects.

I have one brave suggestion :). You have the string expression and the Html.ViewData. You can loop the Html.ViewData.ModelMetadata.Properties recursively and try to get the ModelMetadata for the nested property.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
George K
  • 1,763
  • 1
  • 9
  • 17
  • You are absolutely right. Once I instantiated Company property in the MyViewModel, `ModelMetadata.FromStringExpression` figured out metadata correctly. I'll try your suggestion, but I feel that there are some pitfalls associated with it, or at least you gave me such an impression – Dmitry Efimenko Sep 18 '12 at 09:07
  • I didn't want to gave such an impression. Generally speaking the ModelMetadata is a reflected information from the Model. I believe that you will be able to get the correct ModelMetadata using the suggested approach. Unfortunately I cannot guarantee you that it always will work. At least, you will be able to control the code and not wondering "what the heck happened?". Just my 2cents. – George K Sep 18 '12 at 11:14
  • I didn't try your suggestion yet (going to do that this evening), but another idea has occurred to me meanwhile. We know that getting metadata from lambda expression works for sure. However, we have a string expression. Would you happen to know a way to convert one into another? Or is that a silly thing I'm asking? – Dmitry Efimenko Sep 18 '12 at 17:20
  • Having troubles to do what you suggested. Would you happen to have a sample code? – Dmitry Efimenko Sep 19 '12 at 06:27