简体   繁体   English

如何创建允许用户选择月份的下拉列表?

[英]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.

相关问题 如何在 CSHTML 中使用 angular 创建下拉列表? - How do I create a drop down list using angular inside CSHTML? 如何在ASP.NET MVC 3中为填充的下拉列表创建视图模型 - How do I create a view model for a populated drop down list in ASP.NET MVC 3 如何使用C#在ASP MVC中的下拉列表中创建唯一的日期列表? - How do I create a unique list of dates in a drop down in ASP MVC with C#? 如何使用下拉列表重定向,而不是按钮? - How do I redirect with a Drop Down List, and not a Button? 如何将下拉列表选择发送到控制器 - How do I send drop down list selection to my controller 如何在页面加载时强制下拉列表选择? - How do I force drop down list selections on page load? 如何控制下拉列表中显示的屏幕保护程序名称? - How do I control the screensaver name shown in the drop down list? 如何将包含月份的下拉列表设置为上个月 - How to set Drop down List containing Months to previous month 如何根据另一个下拉列表 ASP.NET 的选择填充下拉列表 - How do I fill a drop down-list based on selection by another drop-down list ASP.NET 如何创建允许用户在3种对象类型之间进行选择的Windows界面表单? - How do I create a Windows Interface Form that allows user to pick between 3 object types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM