[英]How do I create a drop down list that allows a user to choose a month?
Right now I have this and it all works fine. 现在我有了这个,一切都很好。
PossibleExpirationMonths = creditCardInterpreter.GetPossibleMonths()
The GetPossibleMonths method: GetPossibleMonths方法:
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 我遇到的问题是,当我尝试测试方法并访问动态对象的Display和Value属性时,
returnedList.First().Display
I get an error. 我得到一个错误。 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'object' does not contain a definition for 'Display' Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“对象”不包含“显示”的定义
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? 有什么想法/建议吗?
The problem is that .First()
always returns type Object
. 问题在于.First()
始终返回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? 为什么不使用DateTimePicker
或类似的控件? All of the things you're wanting out of this are properties of a DateTime
(specifically, DateTime.Month
) . 您想要的所有东西都是DateTime
属性(特别是DateTime.Month
)。 For the web, this solution works well. 对于网络, 此解决方案效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.