我创建了一个下拉列表,显示所有日子(星期日)从上午9点到下午6点的预选时间为1小时的时间间隔,但我想在星期天搜索很多但是找不到帮助< / p>
这是我的代码
绑定下拉列表
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)
现在我希望在页面加载时,然后在假日
中预先选择星期日的下拉列表更新
我想要那样的视图
答案 0 :(得分:1)
编辑:
您是否在每个下拉列表中使用@Html.DropDownListFor(model => model.MondayStart,ST)
?
对于SundayStart和SundayEnd,它应该使用model.SundayStart或者任何ID为SelectedListItem,其值为“Holiday”
-
尝试明确设置所选值
dropdownlist.SlectedIndex = 9;
或
dropdownlist.SelectedValue = "Holiday";
答案 1 :(得分:1)
您可以为下拉列表指定一个ID,然后使用jQuery进行更新。
查看强>
@Html.DropDownListFor(model => model.MondayStart,ST, new { id = "SundayStartDropdown" })
<强>的jQuery 强>
<script>
$(document).ready(function () {
$('#SundayStartDropdown').val('Holiday');
});
</script>
答案 2 :(得分: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))
这将为您提供下拉列表中的选定项目