Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8543689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:21:59+00:00 2026-06-11T12:21:59+00:00

I am trying to build an html helper that would have access to modelmetadata.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:22:00+00:00Added an answer on June 11, 2026 at 12:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a form in HTML that, once submitted, automatically generates
I'm trying to build a blog/messageboard that uses static HTML files to serve content.
I'm trying to build some HTML with Knockout that Jquery UI can turn into
I'm trying to build a Sanitize transformer that accepts potentially malformed HTML input with
I'm trying to build a jQuery.live like function. Helper is a class that has
I'm trying to build a custom HTML helper. Following several tutorials, my code is
Im trying to build a html table that only contains the table header and
I'm trying my best to build a helper that outputs a <'ul> consisting of
I am trying to use PHP to build HTML that I will later display.
I have following HTML Structure: I am trying to build a robust method to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.