Reputation: 443
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?
Upvotes: 1
Views: 89
Reputation: 1950
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.
Upvotes: 2