0

I am trying to retrieve all different months from a table in a database, but it keeps on giving me System.Collections.Generic.List1[System.String] in text on the front-end instead of the data.

var months = _db.Users.ToList().Select(o => o.dt_created.ToString("MMMM")).ToList();
ViewBag.Months = months;

What am I doing wrong?

9
  • It is giving you where?
    – dymanoid
    Dec 12, 2018 at 16:19
  • Oh, on the front-end in a text.
    – Floor
    Dec 12, 2018 at 16:20
  • This is an important info. Much more, you need to exactly specify which front end.
    – dymanoid
    Dec 12, 2018 at 16:20
  • 2
    It's giving you a list. You need a loop to read the items and display them.
    – Casey
    Dec 12, 2018 at 16:21
  • The default string representation for an object (such as a List<T> for example) is the type name. You'd need to specify how you want to translate that object into a string, either in this code or in the view.
    – David
    Dec 12, 2018 at 16:21

1 Answer 1

5
ViewBag.Months = months;

The above code, the months is a list so how are you dispaying it in the UI, you need to join the list to show it as a string

ViewBag.Months = string.Join(", ", months);

Not the answer you're looking for? Browse other questions tagged or ask your own question.