简体   繁体   English

如何创建可重复使用的下拉列表

[英]How do I create a reusable dropdown list

I'm after creating a really simple drop-down list, which essentially shows various times, and their value is the minutes past midnight for each time. 我创建了一个非常简单的下拉列表,该列表实际上显示了各个时间,它们的值是每次午夜后的分钟数。 For example: 例如:

Value       Time
60          01:00 AM
75          01:15 PM
90          01:30 PM
105         01:45 PM
120         02:00 PM

etc 等等

The thing is, I need to use the same drop-down list in a whole bunch of views and controllers in my application. 关键是,我需要在应用程序中的所有视图和控制器中使用相同的下拉列表。 After reading a couple of articles, it seems like I need to create a static class for this? 看了几篇文章之后,看来我需要为此创建一个静态类吗? But all the examples seem to be way more complicated than I actually need. 但是所有示例似乎都比我实际需要的复杂得多。 Is it an IList that I should be creating for this? 我应该为此创建一个IList吗?

You could try the following method 您可以尝试以下方法

  1. Create a static class that generate data source for the dropdown 创建一个静态类,为该下拉列表生成数据源
public static class DropdownData
{
    public static IList<SelectListItem> GetTimeList(string defaultValue)
    {
        var times = new List<SelectListItem>();
        times = Enumerable.Range(4, 4).Select(x => {
            var minute = x * 15;
            var time = DateTime.Today.AddMinutes(minute);
            return new SelectListItem()
            {
                Value = minute.ToString(),
                Text = time.ToString("hh:mm tt"),
                Selected = (!string.IsNullOrEmpty(defaultValue) && minute.Equal(defaultValue))
            };
        }).ToList();
        return times;
    }
}
  1. In your views, you could create dropdown list with data source that generated in step 1 在您的视图中,您可以使用在步骤1中生成的数据源创建下拉列表
@Html.DropDownList("TimeList", DropdownData.GetTimeList("75"), new { @class = "form-control" })

If you need a value to be Global , then yes, you'll want it to be public static in C#. 如果需要将值设为Global ,则可以,在C#中将其设为public static I recommend also putting it in a public static class so you can access it by name. 我建议还将其放在public static class以便您可以按名称访问它。

public static class MyData
{
     public static IEnumerable<DateTime> ValueTimes { get; set;} //(or IList or List<T> or whatever you want)     
}

public class IDoStuff
{
  public DateTime myTime { get; set; }
  public IDoStuff(DateTime d) //constructor
  {
     if(ValueTimes.IndexOf(d) != -1) //-1 says I no findie
        myTime = d;
     else
        throw new ApplicationException("That's not a valid time, dummy");
  }
}

See, easy peasy. 看,轻松自在。 Also, use DateTime or TimeSpan , even if it's all in the same day. 另外,使用DateTimeTimeSpan ,即使它们都在同一天。 Don't use strings to represent time, you're asking for trouble that way. 不要使用strings来表示时间,而是用这种方式来寻求麻烦。

Edit: While I was putting up the solution, you posted sample data. 编辑:在我提出解决方案时,您发布了示例数据。 Given this: 鉴于这种:

Value       Time
60          01:00 AM
75          01:15 PM
90          01:30 PM
105         01:45 PM
120         02:00 PM

I think you can actually just do this and skip the static variable: 我认为您实际上可以执行此操作并跳过static变量:

public static Timespan GetTime(int value)
{
   return Timespan.FromMinutes(value);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建可重用的UserControl并将命令绑​​定到该控件? - How do I create a reusable UserControl and Bind a Command to it? 如何在WPF中创建可重用的TextBlock绑定? - How do I create a reusable TextBlock binding in WPF? 在 C# 中,如何创建需要访问多个列表类型的可重用方法? - In C#, how can I create a reusable method where I need to access multiple list types? 如何在ViewModel上创建可变长度下拉列表而不重复c#中的代码? - How do I create a variable length dropdown list on a ViewModel without repeating code in c#? 如何将我的列表作为数据源返回到下拉列表控件 - How do I return my List as a DataSource to a dropdown list control 如何统一隐藏下拉列表中的选项? - How do I hide an option in a dropdown list in unity? 如何在ASP下拉列表中添加垂直滚动条? - How do i add vertical scroll bar in asp Dropdown list? 如何使用过去30天的日期填充下拉列表 - How do I populate a dropdown list with the past 30 days of dates 如何创建可重用的Entity Framework投影表达式? - How can I create a reusable Entity Framework projection expression? 如何使搜索/分页在应用程序中可重用? - how do I make searching/pagination reusable in my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM