任何人有asp.net的MVC某些下拉列表日期选择器 [英] anybody has some dropdowns datepicker for asp.net mvc

查看:223
本文介绍了任何人有asp.net的MVC某些下拉列表日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人知道我在哪里可以找到一个HTML帮助或东西是要生成一个从3下拉菜单组成的日期选择器?

anybody knows where can I find a html helper or something that is going to generate a datepicker composed from 3 dropdowns ?

推荐答案

这是我的小帮手。

本身的解释,我相信。可以调整,安排下拉菜单(月/日/年或日/月/年)的顺序,如果你使用.NET 4中,你可以把默认参数的名称。

Itself explanatory, I believe. Can be tweaked to arrange the order of the drop-downs (Month/Day/Year or Day/Month/Year), and if you're using .NET 4, you can put default parameters for the names.

编辑:清理,以减少眼睛出血文

Cleared up the 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());
}

这篇关于任何人有asp.net的MVC某些下拉列表日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆