locked
How to set month from datetime model in dropdownlistfor RRS feed

  • Question

  • User-347328420 posted

    Hi friends! I need help to set month value from datetime in dropdownlistfor in edit view. I've tried to do something like this:

    <%: Html.DropDownListFor( model => model.BIRTHDAY.Value.Month, Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).ToString().First().ToString().ToUpper() + System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).Substring(1)}), "Month") %>

    but value is not selected . How can I do that please?

    Thanks!

    Wednesday, October 12, 2016 8:25 AM

Answers

  • User-347328420 posted

    Solved it by settin value in controller :

    agent.Month = agent.BIRTHDAY.Value.Month;

    and in my view:

    <%: Html.DropDownListFor( model => model.Month, Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).ToString().First().ToString().ToUpper() + System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).Substring(1)}), "Month") %>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 12, 2016 9:09 AM

All replies

  • User-347328420 posted

    Solved it by settin value in controller :

    agent.Month = agent.BIRTHDAY.Value.Month;

    and in my view:

    <%: Html.DropDownListFor( model => model.Month, Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).ToString().First().ToString().ToUpper() + System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).Substring(1)}), "Month") %>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 12, 2016 9:09 AM
  • User-135410578 posted

    Add an ID on your HTML.DropDownListFor:

    new { id = "dtDateMonth" }

    After that, add a script to set the default:

    $('#dtDateMonth').datetimepicker({
    format: 'MM'
    });

    Hi friends! I need help to set month value from datetime in dropdownlistfor in edit view. I've tried to do something like this:

    <%: Html.DropDownListFor( model => model.BIRTHDAY.Value.Month, Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).ToString().First().ToString().ToUpper() + System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i).Substring(1)}), "Month") %>

    but value is not selected . How can I do that please?

    Thanks!

    Wednesday, October 12, 2016 9:17 AM
  • User-347328420 posted

    Thank you for the answer. I've tried your suggestion but can't display the datepicker on dropdownlistfor, unless I use textbox. could you help me for that? thanks

    Wednesday, October 12, 2016 1:49 PM