2013-11-26 6 views
-2

오전 9 시부 터 오후 6 시까 지 모든 시간대 (월 - 일)에서 1 시간 간격으로 시간을 표시하는 드롭 다운 목록을 만들었지 만 검색된 일요일에 휴가를 표시하려고하지만 더 도움이 여기MVC4 드롭 다운 목록에서 선택한 값

를 찾을하는 드롭 다운

public List<SelectListItem> StartTime() 
{ 
    List<SelectListItem> st = new List<SelectListItem>(); 
    st.Add(new SelectListItem() { Value = "12:00 AM", Text = "12:00 AM" }); 
    st.Add(new SelectListItem() { Value = "01:00 AM", Text = "01:00 AM" }); 
    st.Add(new SelectListItem() { Value = "02:00 AM", Text = "02:00 AM" }); 
    st.Add(new SelectListItem() { Value = "03:00 AM", Text = "03:00 AM" }); 
    st.Add(new SelectListItem() { Value = "04:00 AM", Text = "04:00 AM" }); 
    st.Add(new SelectListItem() { Value = "05:00 AM", Text = "05:00 AM" }); 
    st.Add(new SelectListItem() { Value = "06:00 AM", Text = "06:00 AM" }); 
    st.Add(new SelectListItem() { Value = "07:00 AM", Text = "07:00 AM" }); 
    st.Add(new SelectListItem() { Value = "08:00 AM", Text = "08:00 AM" }); 
    st.Add(new SelectListItem() { Value = "09:00 AM", Text = "09:00 AM", Selected=true }); 
    st.Add(new SelectListItem() { Value = "10:00 AM", Text = "10:00 AM" }); 
    st.Add(new SelectListItem() { Value = "11:00 AM", Text = "11:00 AM" }); 
    st.Add(new SelectListItem() { Value = "12:00 PM", Text = "12:00 PM" }); 
    st.Add(new SelectListItem() { Value = "01:00 PM", Text = "01:00 PM" }); 
    st.Add(new SelectListItem() { Value = "02:00 PM", Text = "02:00 PM" }); 
    st.Add(new SelectListItem() { Value = "03:00 PM", Text = "03:00 PM" }); 
    st.Add(new SelectListItem() { Value = "04:00 PM", Text = "04:00 PM" }); 
    st.Add(new SelectListItem() { Value = "05:00 PM", Text = "05:00 PM" }); 
    st.Add(new SelectListItem() { Value = "06:00 PM", Text = "06:00 PM" }); 
    st.Add(new SelectListItem() { Value = "07:00 PM", Text = "07:00 PM" }); 
    st.Add(new SelectListItem() { Value = "08:00 PM", Text = "08:00 PM" }); 
    st.Add(new SelectListItem() { Value = "09:00 PM", Text = "09:00 PM" }); 
    st.Add(new SelectListItem() { Value = "10:00 PM", Text = "10:00 PM" }); 
    st.Add(new SelectListItem() { Value = "11:00 PM", Text = "11:00 PM" }); 
    st.Add(new SelectListItem() { Value = "Holiday", Text = "Holiday" }); 
    return st; 
} 

보기

,536,913,632 바인딩 내 코드

입니다 10

TimeFunctions tf = new TimeFunctions(); List<SelectListItem> ST = tf.StartTime(); @Html.DropDownListFor(model => model.MondayStart,ST) 

는 지금은 페이지가로드 한 후 일요일의 드롭 홀리데이 WID 미리 선택하는 경우

내가 그

enter image description here

+1

은 그런 대신에 "휴일"항목에 대한 true로'Selected' 설정 드롭 다운 목록에서 선택한 항목을 줄 것이다? (또는 모델에 "휴일"데이터를 설정하는 것이 더 좋습니다.) 나는 당신이 무엇을 물어 보려고하는지 알지 못합니다. –

+0

@lc. 업데이트 된 질문을 확인하십시오 – Mohsin

+0

@MohsinMustufa Ic가 여전히 올바름 ... 일요일 바인딩 목록에서 Holiday의'SelectListItem.Selected'' 속성을 true로 설정하십시오. –

답변

1

또는

드롭 다운 목록을 누른 다음 jQuery를 사용하여 업데이트하십시오.

당신은 다음과 같이 할 수

<script> 
    $(document).ready(function() { 
     $('#SundayStartDropdown').val('Holiday'); 
    }); 
</script> 
1

편집과 같은보기를 원하는 업데이트 된 것을 원하는 :

각 드롭 다운 목록에 대해?

선택된 값

dropdownlist.SlectedIndex = 9; 
을 설정 명시 적으로 시도 -

SundayStart를 들어

과는 ID가 "휴일"의 가치와 SelectedListItem에 대한

무엇이든 model.SundayStart 이상을 사용한다 SundayEnd 당신은에 ID를 제공 할 수 있습니다

dropdownlist.SelectedValue = "Holiday"; 
+0

MVC를 사용하는 Brom이 단순하지 않습니다. Asp.net – Mohsin

+0

문제가 해결되었습니다. ive가 'Controller'에서 SundayStart와 Holiday to End의 값을 만든 후 Model에 전달했습니다. ' – Mohsin

0

보기

@Html.DropDownListFor(model => model.MondayStart,ST, new { id = "SundayStartDropdown" }) 

jQuery를 :

내 모델 :

namespace MVCMusicStore.Models 
{ 
    public class Login 
    { 
     [Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")] 
     public string Name { get; set; } 

     [Required(AllowEmptyStrings = false, ErrorMessage = "User Name is required")] 
     public string UserName { get; set; } 

     [DataType(DataType.Password)] 
     [Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")] 
     public string Password { get; set; } 

     [DataType(DataType.Password)] 
     [Required(AllowEmptyStrings = false, ErrorMessage = "Confirm Password is required")] 
     public string ConfirmPassword { get; set; } 

     public string CountryList { get; set; } 

     public List<SelectListItem> Country 
     { 
      get; 
      set; 
     } 

     public string SelectedCountry { get; set; } 
     public Login() 
     { 
      Bindcountry(); 
     } 

     public void Bindcountry() 
     { 
      List<SelectListItem> coutryList = new List<SelectListItem>(); 
      coutryList.Add(new SelectListItem { Text = "India", Value = "India" }); 
      coutryList.Add(new SelectListItem { Text = "USA", Value = "USA" }); 
      coutryList.Add(new SelectListItem { Text = "UK", Value = "UK"}); 
      coutryList.Add(new SelectListItem { Text = "Mexico", Value = "Mexico" }); 
      coutryList.Add(new SelectListItem { Text = "Germany", Value = "Germany", Selected = true }); 
      coutryList.Add(new SelectListItem { Text = "France", Value = "France" }); 


      this.Country = coutryList; 
      SelectedCountry = "Mexico"; 

     } 

    } 
} 

보기 :

@Html.DropDownListFor(m => m.CountryList, new SelectList(Model.Country,"Value","Text",Model.SelectedCountry)) 

이 당신에게

관련 문제