2013-11-26 163 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)) 

这将使你在下拉列表中选择的项目