I’m trying to bind an enum AgeRange using Html.DropDownListFor but regardless of whatever I’m choosing from View page, Controller is getting a value ‘0’. Can anyone help me to fix this issue?
EDIT: Controller code in place.
Enum Class:
public enum AgeRange
{
Unknown = -1,
[Description("< 3 days")]
AgeLessThan3Days = 1,
[Description("3-6 days")]
AgeBetween3And6 = 2,
[Description("6-9 days")]
AgeBetween6And9 = 3,
[Description("> 9 days")]
AgeGreaterThan9Days = 4
}
View:
@Html.DropDownListFor(
model => model.Filter.AgeRangeId,
@Html.GetEnumDescriptions(typeof(AgeRange)),
new { @class = "search-dropdown", name = "ageRangeId" }
)
Controller:
public ActionResult Search(int? ageRangeId)
{
var filter = new CaseFilter { AgeRangeId = (AgeRange)(ageRangeId ?? 0) };
}
You’re close…
I’d suggest following along with this guy