1

Right now I have this and it all works fine.

PossibleExpirationMonths = creditCardInterpreter.GetPossibleMonths()

The GetPossibleMonths method:

    public List<dynamic> GetPossibleMonths()
    {
        return new List<dynamic>()
        {
            new {Display = "01 Jan", Value = 1 },
            new {Display = "02 Feb", Value = 2 },
            new {Display = "03 Mar", Value = 3 },
            new {Display = "04 Apr", Value = 4 },
            new {Display = "05 May", Value = 5 },
            new {Display = "06 Jun", Value = 6 },
            new {Display = "07 Jul", Value = 7 },
            new {Display = "08 Aug", Value = 8 },
            new {Display = "09 Sep", Value = 9 },
            new {Display = "10 Oct", Value = 10 },
            new {Display = "11 Nov", Value = 11 },
            new {Display = "12 Dec", Value = 12 }
        };
    }

and how I'm displaying it:

@Html.DropDownListFor(model => model.ExpirationMonth, new SelectList(Model.PossibleExpirationMonths, "Value", "Display"), new { @class = "form-control" })

The problem I'm running into is that when I try to test the method and access the Display and Value properties of the dynamic like

returnedList.First().Display

I get an error. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'object' does not contain a definition for 'Display'

The reason I want to use dynamics here is because I don't feel like I should create a container class that contains such a little amount of information. It just creates bloat and I'm TRYING to be cleaner.

Any thoughts/suggestions?

Jesse Moreland
  • 441
  • 6
  • 17
  • How are you populating `returnedList`? – stuartd Jan 10 '17 at 17:44
  • 1
    Well, I'm sorry you feel that way. Use `KeyValuePair` instead if you really want to reduce lines of code at any cost. Did `List` fail the same way? – 15ee8f99-57ff-4f92-890c-b56153 Jan 10 '17 at 17:44
  • ...at the end of the day, though, I've been doing this for a living for 20 years and I see nothing wrong with writing a quickie class for that purpose. This is nuclear over-thinking. – 15ee8f99-57ff-4f92-890c-b56153 Jan 10 '17 at 17:47
  • I'm not saying that writing a container class is wrong. I'm just doing something that would make the solution explorer have one file less, I'm not tied to this way of doing it. – Jesse Moreland Jan 10 '17 at 17:50
  • 1
    Why are you using `List` instead of `List` which is what your ultimately need anyway? And it can be as simple as `public IEnumerable Months() { return Enumerable.Range(1, 12).Select(x => new SelectListItem() { Value = x.ToString(), Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeForm‌at.GetMonthName(x) }); }` –  Jan 10 '17 at 19:59

1 Answers1

2

The problem is that .First() always returns type Object. You can try:

((dynamic)returnedList.First()).Display

I think you might be re-inventing the wheel a bit, though. Why aren't you using a DateTimePicker or similar control? All of the things you're wanting out of this are properties of a DateTime (specifically, DateTime.Month) . For the web, this solution works well.

Community
  • 1
  • 1
CDove
  • 1,940
  • 10
  • 19
  • Mmmm, First() was my way of trying to simplify it for the sake of the question. I still cannot access .Display off an object cast to a dynamic. – Jesse Moreland Jan 10 '17 at 17:51