Does anyone have a datepicker dropdown for asp.net mvc

Does anyone know where I can find an html helper or something that will generate a datepicker consisting of 3 drop down menus?

+3
source share
2 answers

This is my little helper.

Self explanatory, I think. You can customize the order of the drop-down lists (month / day / year or day / month / year), and if you use .NET 4, you can put the default settings for the names.

Edit: Cleared of text to reduce eye bleeding

/// <summary>
/// Creates a days, months, years drop down list using an HTML select control. 
/// The parameters represent the value of the "name" attribute on the select control.
/// </summary>
/// <param name="dayName">"Name" attribute of the day drop down list.</param>
/// <param name="monthName">"Name" attribute of the month drop down list.</param>
/// <param name="yearName">"Name" attribute of the year drop down list.</param>
/// <returns></returns>
public static string DatePickerDropDowns(this HtmlHelper html, string dayName, string monthName, string yearName)
{
    TagBuilder daysList = new TagBuilder("select");
    TagBuilder monthsList = new TagBuilder("select");
    TagBuilder yearsList = new TagBuilder("select");

    daysList.Attributes.Add("name", dayName);
    monthsList.Attributes.Add("name", monthName);
    yearsList.Attributes.Add("name", yearName);

    StringBuilder days = new StringBuilder();
    StringBuilder months = new StringBuilder();
    StringBuilder years = new StringBuilder();

    int beginYear = DateTime.UtcNow.Year - 100;
    int endYear = DateTime.UtcNow.Year;

    for (int i = 1; i <= 31; i++)
        days.AppendFormat("<option value='{0}'>{0}</option>", i);

    for (int i = 1; i <= 12; i++)
        months.AppendFormat("<option value='{0}'>{0}</option>", i);

    for (int i = beginYear; i <= endYear; i++)
        years.AppendFormat("<option value='{0}'>{0}</option>", i);

    daysList.InnerHtml = days.ToString();
    monthsList.InnerHtml = months.ToString();
    yearsList.InnerHtml = years.ToString();

    return string.Concat(daysList.ToString(), monthsList.ToString(), yearsList.ToString());
}
+3
source

Telerik has a library of some ASP.Net MVC controls, which is free.

. , DatePicker :

<%= Html.Telerik().DatePicker()
        .Name("DatePicker")
        .MinDate(Model.MinDate.Value)
        .MaxDate(Model.MaxDate.Value)
        .Value(Model.SelectedDate.Value)
        .ShowButton(Model.ShowButton.Value)
%>
-1

Source: https://habr.com/ru/post/1737343/


All Articles