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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:30:57+00:00 2026-05-29T23:30:57+00:00

I have a viewmodel where the properties have display names. I am now making

  • 0

I have a viewmodel where the properties have display names. I am now making an Excel sheet with the same data in as the grid in the webpage. I am using closed xml (which uses openxml) to create the Excelsheet on the server and send it to the webclient when they want to download data as Excel.
When I write the header row in Excel openxml stream I want to reuse the displayname that I already have defined. But I can’t figure out what to call on.

Here is an example of the display name. ( For the example I only use two, reality there are many many more columns) :

 using System.ComponentModel.DataAnnotations;
 public class DCArrival : IDCArrival
 { 
   [Display(Name = "Via Transit")]
   public String LocationType { get; set; }
   [Display(Name = "Currency")]
   String CurrencyISOCode { get;  }
 }

Then I want to use this displayname when I create the header row. Mark the pseudocode that tries to explain what I want to get hold of:

private MemoryStream CreateExcelFile(ICollection<DCArrival> dcArrToShow
, QueryStrInput   queryStrInput)
    {
        try
        {
            // Create an Excel Workbook
            XLWorkbook wb = new XLWorkbook();
            // Add the worksheet with  data 
            IXLWorksheet ws = wb.Worksheets.Add("New Worksheet");
            // Add my data that was displayed in the html table ... 
            ws.Cell(1, 1).SetValue("Hello World");
           //Add Header row. By taking a object in the collection and figure out its
           // display name
           DCArrival firstRow = dcArrToShow.First();
           // Here comes my problem. Here is my dream up mockup code

            ws.Cell(2,1).Value = firstRow.LocationType.DisplayAttribute.GetName()
            ws.Cell(2,2).Value = firstRow.CurrencyISOCode.DisplayAttribute.GetName()
           // back to reality
           //this is how easy I can get all data from Collection
            ws.Cell(3, 1).Value = dcArrToShow.AsEnumerable();
          // All done
            MemoryStream ms = new MemoryStream();
            wb.SaveAs(ms);
            return ms;
        }
        catch (Exception e)
        {
            string errmsg = String.Format("Failed to create Excel file: {0}",
          e.Message);
            throw new Exception(errmsg, e);
        }

I have looked at the http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.aspx
But I do not understand how I get a hold of it.
I understand that this is magically fixed for you when you are using normal mvc 3 html rendering. When I Google these concepts I get drowned in blogs who wants to explain basic validation in MVC 3.

  • 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-29T23:30:59+00:00Added an answer on May 29, 2026 at 11:30 pm

    You could retrieve it from the model metadata:

    ws.Cell(2.1).Value = ModelMetadata.FromLambdaExpression<DCArrival, string>(x => x.LocationType, new ViewDataDictionary<DCArrival>(firstRow)).DisplayName;
    

    or write an extension method:

    public static class ModelMetadataExtensions
    {
        public static string GetName<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty>> ex)
        {
            return ModelMetadata
                .FromLambdaExpression<TModel, TProperty>(ex, new ViewDataDictionary<TModel>(model))
                .DisplayName;
        }
    }
    

    and then:

    ws.Cell(2.1).Value = firstRow.GetName(x => x.LocationType);
    ws.Cell(2.2).Value = firstRow.GetName(x => x.CurrencyISOCode);
    

    UPDATE:

    As per a new requirement that was expressed in the comments section in order to loop through all properties and read the display attribute you could use the following:

    var properties = typeof(DCArrival).GetProperties(); 
    foreach (var property in properties) 
    {
        var displayAttribute = property
            .GetCustomAttributes(typeof(DisplayAttribute), true)
            .FirstOrDefault() as DisplayAttribute;
        string displayName = property.Name;
        if (displayAttribute != null)
        {
            displayName = displayAttribute.Name;
        }
    
        // TODO: do something with the display name for this property
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a viewmodel that contains a number of properties, a SelectList, and an
I have a viewmodel class for a create view that consists of 2 properties
I have a ViewModel like this: public class MyViewModel { [Display(Name = One_Name, ResourceType
The idea (simplified) is to have user definable properties in ViewModel contained in a
In ASP.NET MVC 3 web application I have a viewmodel with properties which marked
I have seen a lot of trivial examples of accessing ViewModel properties in the
I have a strongly typed view that is using a viewmodel I created. I
I have a user control let say UC1 . This user control have viewmodel
I am trying to implement Repository pattern in JavaScript. I have ViewModel which i
Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a

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.