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 6711605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:09:38+00:00 2026-05-26T08:09:38+00:00

Should I be using some other attrbute that the List template respects, or am

  • 0

Should I be using some other attrbute that the List template respects, or am I stuck having to manually change the source to DisplayFor(m => m.someProperty) ?

I read in another thread a suggestion that ExpressionHelper.GetExpressionText should be used, but that’s not exactly what I’m looking for. I want the IDE’s Add View wizard to work for Lists as it does for the other scaffold templates, and I’m hoping there’s an attribute i could use on my properties to get that done.

  • 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-05-26T08:09:38+00:00Added an answer on May 26, 2026 at 8:09 am

    It’s because the list could be empty and with an empty list you have no access to the an instance of the type you’re getting the display name for. As a result you lose typed access to the particular property you’re referencing.

    For instance how would you know which property to use the display name for if you don’t have access to the object

    <th>@Html.DisplayFor(f => f....,
                /* 
                  how do you get the property you want to
                  access here if the list is empty
                */
        )</th>
    

    You could write an extension method that would accept the model type of course but you’ll lose that typed access.

    public static IHtmlString DisplayType(this HtmlHelper html, Type modelType, string propertyName) {
        object modelInstance = Activator.CreateInstance(modelType);
        var data = ModelMetadata.FromStringExpression(propertyName, 
                          new ViewDataDictionary(modelInstance));
        return new HtmlString(data.DisplayName ?? propertyName);
    }
    
    
    @Html.DisplayType(typeof(CoolModel), "Name")
    

    Though I don’t really recommend it.

    small update – you could also access it via the model as well but still without typed access:

    public static IHtmlString DisplayType<TModel, TValue>(this HtmlHelper<TModel> html,
              Expression<Func<TModel, TValue>> expression, 
              string propertyName) where TModel : IEnumerable {
        object modelInstance = 
                  Activator.CreateInstance(expression.Body.Type.GetGenericArguments()[0]);
    
        var data = ModelMetadata.FromStringExpression(propertyName, 
                              new ViewDataDictionary(modelInstance));
        return new HtmlString(data.DisplayName ?? propertyName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[1] In JDBC, why should we first load drivers using Class.forName(some driver name). Why
When exposing some code to D-Bus using Qt D-Bus bindings, when should one use
What are some situations where languages should be mixed? I'm not talking about using
I'd like to know situations in which I should consider using a framework other
Should developers avoid using continue in C# or its equivalent in other languages to
I'm developing an iPhone application. The application access some web service that aboug other
Is there a specific reason why I should be using the Html.CheckBox , Html.TextBox
I'm trying to find out whether I should be using business critical logic in
Right, perhaps I should be using the normal Python lists for this, but here
Are there any blogs, guides, checklists, or controls we should be using to ensure

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.