2013-11-26 161 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; 
} 

查看

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

現在我想的是,當頁面加載,然後在週日的下拉菜單中預選WID假日

更新

我想這樣的

enter image description here

+1

然後設置'Selected'爲true「假日」項目呢? (或更好地將數據設置爲您的模型上的「假日」)我不知道你想問什麼。 –

+0

@lc。檢查更新後的問題 – Mohsin

+0

@MohsinMustufa Ic仍然正確...在星期日綁定列表中將Holiday「SelectListItem.Selected」屬性設置爲true。 –

回答

1

你可以給一個ID下拉列表,然後使用jQuery來更新它。

查看

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

jQuery的

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

編輯觀點:您是否在使用@Html.DropDownListFor(model => model.MondayStart,ST)爲每個下拉列表?

對於SundayStart和SundayEnd應該使用model.SundayStart或任何ID是SelectedListItem與 「假日」 的價值

-

嘗試明確設置所選值

dropdownlist.SlectedIndex = 9; 

dropdownlist.SelectedValue = "Holiday"; 
+0

使用MVC的宏不是簡單的Asp.net – Mohsin

+0

問題解決了 我在'Controller'中創建了SundayStart和End to Holiday的值,然後將它傳遞給'Model ' – Mohsin

0

你可以這樣做:

我的模型:

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)) 

這將使你在下拉列表中選擇的項目