1

I am having following DropDown which populates from Enum.

@Html.DropDownListFor(model => model.Month1, Enum.GetNames(typeof(Models.InputMonths)).Select(e => new SelectListItem { Text = e }), "--Select Month--", new { @class = "form-control", @id = "Month1" })

public enum InputMonths
{
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
}

The output of html is like this <option>January</option>

I want value of the month in numeric like this <option value="1">January</option>

How will the value attribute come with numeric values?

1 Answer 1

1

You have to specify Value in new SelectListItem { Text = e, Value = enumValue }

EDIT

List<InputMonths> months = Enum.GetValues(typeof(InputMonths)).Cast<InputMonths>().ToList();
@Html.DropDownListFor(model => model.Month1, months.Select(e => new SelectListItem { Text = e, Value = (int)e }), "--Select Month--", new { @class = "form-control", @id = "Month1" })
Sign up to request clarification or add additional context in comments.

2 Comments

Shows me error CS0103: The name 'enumValue' does not exist in the current context
It's just example. You have to specify your own variable.

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.