2014-09-20 2 views
0

내 드롭 다운은 다음 코드로 제대로 작동합니다. jQuery Script에서 필요한 유효성 검사를 확인할 수 있도록.MVC - - DropDownListFor 열거 설정 기본값

어떻게 하시겠습니까? 당신의 열거에

+0

는'StopMonth'의 널 (NULL)을? –

+0

예 ..'StopMonth'에'null '을 설정할 수 있습니다. – Anup

+1

세 번째 매개 변수가있는 오버로드를 "--Select month--"[참조 문서] (http://msdn.microsoft.com/en-us/library/ee703567(v=118))로 사용자에게 알릴 수 있습니다. aspx). 확실하지는 않지만'SelectList'의'Value'와'Text' 속성도 설정해야한다고 생각합니다 –

답변

2

먼저 추가 값

public enum InputMonths { 
    January = 1, 
    February = 2, 
    March = 3, 
    April = 4, 
    May = 5, 
    . 
    . 
    .   
} 

그런 다음 귀하의 모델 장식 : 마지막으로

[Required] 
[Range(1, 12, ErrorMessage = "Select a month"))] 
public StopMonth StopMonth { get; set; } 

그리고 :

@Html.DropDownListFor(
    model => model.StopMonth, 
    Enum.GetNames(typeof(Models.InputMonths)) 
     .Select(e => new SelectListItem { Text = e }), 
    "-- Select a month --", 
    new { @class = "form-control"}) 
+0

Thankx ..! 쉬웠 어.! – Anup