Last post Dec 17, 2019 08:52 AM by Yang Shen
Member
18 Points
41 Posts
Dec 17, 2019 06:17 AM|loraine26|LINK
Hi,
I am doing an appointment system web project and what I need to do is to have a dropdown list with selection of time range with interval.
For example,
Start Time: 7:00 AM
End Time: 9:00 AM
Interval: 15 minutes
What I must show in dropdown list is like this..
Can someone help me through on how to do this?
Thanks in advance.
Dec 17, 2019 08:44 AM|loraine26|LINK
Already found the solution to this problem. This is what I did.
var startTime = DateTime.Parse("7:00"); var endTime = DateTime.Parse("9:00"); List<string> time_list = new List<string>(); while (startTime < endTime) { time_list.Add(startTime.ToShortTimeString() + " - " + startTime.AddMinutes(15).ToShortTimeString()); startTime = startTime.AddMinutes(15); } foreach (var a in time_list) { System.Console.WriteLine(a); }
Thank you.
Contributor
3140 Points
983 Posts
Dec 17, 2019 08:52 AM|Yang Shen|LINK
Hi loraine26,
You can use multiple ways to bind data for dropdownlist in Asp.net mvc dynamically. Please check Different Ways Of Binding Razor DropdownList In ASP.NET MVC 5.
I think the confusing part for you is how to add 15 minutes for the time, you can check DateTime.AddMinutes method.
Also, please check below demo using the ViewBag to bind the data:
cshtml:
<html> <head> <meta name="viewport" content="width=device-width" /> <title>DDLDemo</title> </head> <body> <div> @Html.DropDownList("DateDDL", ViewBag.DDLList as IEnumerable<SelectListItem>) </div> </body> </html>
controller:
public ActionResult DDLDemo() { DateTime start = new DateTime(2019, 12, 17, 7, 0, 0); DateTime end = new DateTime(2019, 12, 17, 9, 0, 0); int i = 0; List<SelectListItem> list = new List<SelectListItem>(); while (start.AddMinutes(15) <= end) { list.Add(new SelectListItem() { Text= start.ToString("t")+" - "+start.AddMinutes(15).ToString("t"), Value=i.ToString()}); start = start.AddMinutes(15); i += 1; } ViewBag.DDLList = list; return View(); }
Below is the result of this demo:
Best Regard,
Yang Shen
Member
18 Points
41 Posts
Populate dropdown list with time range selection with 15 minute interval + mvc + c#
Dec 17, 2019 06:17 AM|loraine26|LINK
Hi,
I am doing an appointment system web project and what I need to do is to have a dropdown list with selection of time range with interval.
For example,
Start Time: 7:00 AM
End Time: 9:00 AM
Interval: 15 minutes
What I must show in dropdown list is like this..
Can someone help me through on how to do this?
Thanks in advance.
Member
18 Points
41 Posts
Re: Populate dropdown list with time range selection with 15 minute interval + mvc + c#
Dec 17, 2019 08:44 AM|loraine26|LINK
Hi,
Already found the solution to this problem. This is what I did.
Thank you.
Contributor
3140 Points
983 Posts
Re: Populate dropdown list with time range selection with 15 minute interval + mvc + c#
Dec 17, 2019 08:52 AM|Yang Shen|LINK
Hi loraine26,
You can use multiple ways to bind data for dropdownlist in Asp.net mvc dynamically. Please check Different Ways Of Binding Razor DropdownList In ASP.NET MVC 5.
I think the confusing part for you is how to add 15 minutes for the time, you can check DateTime.AddMinutes method.
Also, please check below demo using the ViewBag to bind the data:
cshtml:
<html> <head> <meta name="viewport" content="width=device-width" /> <title>DDLDemo</title> </head> <body> <div> @Html.DropDownList("DateDDL", ViewBag.DDLList as IEnumerable<SelectListItem>) </div> </body> </html>
controller:
Below is the result of this demo:
Best Regard,
Yang Shen