我试图通过checklistbox检查foreach循环并返回所选的ietms的ID,并将它们设置为我的ViewModel中的Month属性。到目前为止,我已经在屏幕上显示了checklistbox并在ViewModel中获得了正确的参数(我希望如此)。目前,foreach语句因列表为空而出现错误。我该如何解决这个问题?
这是控制器:
public class SchedulerController : Controller
{
// GET: Scheduler
[HttpGet]
public ActionResult SchedulerIndex()
{
List<Values> lst = new List<Values>();
List<Values> lst2 = new List<Values>();
List<Values> lst3 = new List<Values>();
foreach (Values.Months month in Enum.GetValues(typeof(Values.Months)))
{
lst.Add(new Values { Month = month, IsChecked = false });
}
foreach (Values.DaysOfTheWeek day in Enum.GetValues(typeof(Values.DaysOfTheWeek)))
{
lst2.Add(new Values { DaysOfWeek = day, IsChecked = false });
}
//var DaysOfTheMonth = Enumerable.Range(1, 31);
//foreach (var days in DaysOfTheMonth)
//{
// lst3.Add(new Values { DayOfMonth = days, IsChecked = false });
//}
Values val = new Values();
val.Jobs = new List<SelectListItem>();
val.Jobs.Add(new SelectListItem() { Text = "Email", Value = "1", Selected = false });
val.Jobs.Add(new SelectListItem() { Text = "Backup", Value = "2", Selected = false });
val.Jobs.Add(new SelectListItem() { Text = "Start Application", Value = "3", Selected = false });
val.Jobs.Add(new SelectListItem() { Text = "Job4", Value = "4", Selected = false });
val.Jobs.Add(new SelectListItem() { Text = "Job5", Value = "5", Selected = false });
List<Values> month11 = new List<Values>();
month11.Add(new Values { monthID = 0, monthName = "Jan", IsChecked = false });
month11.Add(new Values { monthID = 1, monthName = "Feb", IsChecked = false });
month11.Add(new Values { monthID = 2, monthName = "Mar", IsChecked = false });
month11.Add(new Values { monthID = 3, monthName = "Apr", IsChecked = false });
month11.Add(new Values { monthID = 4, monthName = "May", IsChecked = false });
month11.Add(new Values { monthID = 5, monthName = "Jun", IsChecked = false });
month11.Add(new Values { monthID = 6, monthName = "Jul", IsChecked = false });
month11.Add(new Values { monthID = 7, monthName = "Aug", IsChecked = false });
month11.Add(new Values { monthID = 8, monthName = "Sep", IsChecked = false });
month11.Add(new Values { monthID = 9, monthName = "Oct", IsChecked = false });
month11.Add(new Values { monthID = 10, monthName = "Nov", IsChecked = false });
month11.Add(new Values { monthID = 11, monthName = "Dec", IsChecked = false });
List<Values> day6 = new List<Values>();
day6.Add(new Values { dayID = 0, dayName = "Mon", IsChecked = false });
day6.Add(new Values { dayID = 1, dayName = "Tue", IsChecked = false });
day6.Add(new Values { dayID = 2, dayName = "Wed", IsChecked = false });
day6.Add(new Values { dayID = 3, dayName = "Thu", IsChecked = false });
day6.Add(new Values { dayID = 4, dayName = "Fri", IsChecked = false });
day6.Add(new Values { dayID = 5, dayName = "Sat", IsChecked = false });
day6.Add(new Values { dayID = 6, dayName = "Sun", IsChecked = false });
val.MonthInfo = month11;
val.DayInfo = day6;
ViewBag.lst = lst;
ViewBag.lst2 = lst2;
ViewBag.lst3 = lst3;
return View(val);
}
[HttpPost]
public ActionResult ScheduleInfo(int Job, int Second, int Minute, int Hour, object Month, object DaysOfWeek)//, string Month, string DaysOfWeek)// int DayOfMonth)
{
var val = new Values();
val.Job = Job;
val.Second = Second;
val.Minute = Minute;
val.Hour = Hour;
foreach(var month in val.MonthInfo)
{
if (month.IsChecked == true)
{
val.Month = month.monthID;
}
}
foreach (var day in val.DayInfo)
{
if (day.IsChecked == true)
{
val.DaysOfWeek = day.dayID;
}
}
//val.Month = (int)((Values.Months)Enum.Parse(typeof(Values.Months), Month));
//val.DaysOfWeek = (int)((Values.DaysOfTheWeek)Enum.Parse(typeof(Values.DaysOfTheWeek), DaysOfWeek));
// val.DayOfMonth = DayOfMonth;
return View();
}
}
这是ViewModel(我不使用枚举,我计划摆脱它):
public enum Months
{
Jan = 0,
Feb = 1,
Mar = 2,
Apr = 3,
May = 4,
Jun = 5,
Jul = 6,
Aug = 7,
Sep = 8,
Oct = 9,
Nov = 10,
Dec = 11
}
public enum DaysOfTheWeek
{
Mon = 0,
Tue = 1,
Wed = 2,
Thu = 3,
Fri = 4,
Sat = 5,
Sun = 6
}
public int DayOfMonth { get; set; }
public object Month { get; set; }
public object DaysOfWeek { get; set; }
public bool IsChecked { get; set; }
public int Second { get; set; }
public int Minute { get; set; }
public int Hour { get; set; }
public int Job { get; set; }
public List<SelectListItem> Jobs { get; set; }
public int monthID { get; set; }
public string monthName { get; set; }
public List<Values> MonthInfo { get; set; }
public int dayID { get; set; }
public string dayName { get; set; }
public List<Values> DayInfo { get; set; }
}
以下是观点:
@using (Html.BeginForm("ScheduleInfo", "Scheduler", FormMethod.Post))
{
<center>
<div style="text-align: center">
<div class="form-group">
@Html.DropDownListFor(m => m.Job, Model.Jobs)
</div>
</div>
</center>
<center>
<div class="form-group" style="display:inline-block; padding-right:100px">
<div class=" col-md-2">
@Html.LabelFor(model => model.Second, "Second")
@Html.TextBoxFor(model => model.Second)
</div>
</div>
<div class="form-group" style="display:inline-block; padding-right:100px">
<div class=" col-md-2">
@Html.LabelFor(model => model.Minute, "Minute")
@Html.TextBoxFor(model => model.Minute)
</div>
</div>
<div class="form-group" style="display:inline-block; padding-right:100px">
<div class=" col-md-2">
@Html.LabelFor(model => model.Hour, "Hour")
@Html.TextBoxFor(model => model.Hour)
</div>
</div>
</center>
<br />
<br />
<center>
<div class="col-1">
<ul class="list-group">
<li class="list-group-item-heading list-group-item active">
<h4 class="list-group-item-text">Select the month(s) the task should be set at</h4>
</li>
@Html.HiddenFor(m => m.monthID)
@Html.DisplayFor(m => m.monthName)
@for (int i = 0; i < Model.MonthInfo.Count; i++)
{
<li class="list-group-item" style="display:inline-block">
<div class="checkbox-inline">
@Html.HiddenFor(m => m.MonthInfo[i].monthID)
@Html.CheckBoxFor(m => m.MonthInfo[i].IsChecked)
@Html.LabelFor(m => m.MonthInfo[i].IsChecked, Model.MonthInfo[i].monthName)
</div>
</li>
}
</ul>
</div>
</center>
<center>
<div class="col-2">
<ul class="list-group">
<li class="list-group-item-heading list-group-item active">
<h4 class="list-group-item-text">Select the day(s) the task should be set at</h4>
</li>
@Html.HiddenFor(m=>m.dayID)
@Html.DisplayFor(m=>m.dayName)
@for(int i = 0; i < Model.DayInfo.Count; i++)
{
<li class="list-group-item" style="display:inline-block">
<div class="checkbox-inline">
@Html.HiddenFor(m => m.DayInfo[i].dayID)
@Html.CheckBoxFor(m => m.DayInfo[i].IsChecked)
@Html.LabelFor(m => m.DayInfo[i].IsChecked, Model.DayInfo[i].dayName)
</div>
</li>
}
</ul>
</div>
</center>
<input type="submit" value="Submit Data" id="btnSubmit" />
}