0

I am creating a drop down list of months for a registration form and I have used an enumerable range (0-12) for the database. The problem is, I'm trying to add month names to make the form more user friendly, but I don't know how.

Here's the variable:

var months = Enumerable.Range(1, 12).Select(x => new SelectListItem { Value = x.ToString("0"), Text = x.ToString() });

Here's the dropdown list:

@Html.DropDownList("", months, "Month", new { @id = "monthDropdown", @class = "selectpicker" })

How do I bind a string to each of the numbers in the range?

1 Answer 1

1

Add an array to store months:

var monthArray = new string[] { "January", "February", "March", "April", "May" ..}

Then change your Select statement:

var months = Enumerable.Range(1, 12)
.Select(x => new SelectListItem { Value = x.ToString("0"), Text = monthArray[x-1] });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.