1

AgeRange을 사용하여 Html.DropDownListFor을 사용하지만 [보기] 페이지에서 무엇을 선택하든 관계없이 컨트롤러의 값은 '0'이됩니다. 누구든지이 문제를 해결하도록 도와 줄 수 있습니까?Html.DropDownList 선택된 값에 관계없이 항상 0을 반환합니다.

EDIT : 컨트롤러 코드가 있습니다.

열거 클래스 :

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 
} 

보기 :

@Html.DropDownListFor(
     model => model.Filter.AgeRangeId, 
     @Html.GetEnumDescriptions(typeof(AgeRange)), 
     new { @class = "search-dropdown", name = "ageRangeId" } 
) 

컨트롤러 : 당신이 가까이있어

public ActionResult Search(int? ageRangeId) 
{ 
    var filter = new CaseFilter { AgeRangeId = (AgeRange)(ageRangeId ?? 0) }; 
} 
+0

난 당신이 내가 편집 닫는 브래킷이 필요하다고 제안 사용 –

답변

1

선택 목록을 작동 시키려면 확장 방법을 작성해야합니다.

나는이

public static SelectList ToSelectList<TEnum>(this TEnum enumeration) where TEnum : struct 
{ 
    //You can not use a type constraints on special class Enum. 
    if (!typeof(TEnum).IsEnum) 
    throw new ArgumentException("TEnum must be of type System.Enum"); 
    var source = Enum.GetValues(typeof(TEnum)); 
    var items = new Dictionary<object, string>(); 
    foreach (var value in source) 
    { 
    FieldInfo field = value.GetType().GetField(value.ToString()); 
    DisplayAttribute attrs = (DisplayAttribute)field.GetCustomAttributes(typeof(DisplayAttribute), false).First(); 
    items.Add(value, attrs.GetName()); 
    } 
    return new SelectList(items, Constants.PropertyKey, Constants.PropertyValue, enumeration); 
} 
관련 문제