我正在使用内部网,特别是在每周监控工具上,让我可以逐周查看我的员工的活动。我正在处理一周一周过滤器。到目前为止,我已经找到了如何以下列格式创建包含2015年每周的List
:
From dd/mm/yyyy to dd/mm/yyyy
像这样:
public ActionResult WeeklyMonitoring()
{
ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));
List<string> wl = pr.FetchWeeks(DateTime.Now.Year); // Creates the list
ViewBag.Weeks = wl;
wl.Select(m => new SelectListItem { Text = m, Value = m });
return View();
}
但问题是,当我在DropDownList
中选择一周时(我必须将其转换为正确的类型):
<td><%: Html.DropDownList("Weeks", ((List<string>)ViewBag.Weeks).Select(m => new SelectListItem { Text = m, Value = m }), new { onchange = "this.form.submit();"}) %></td>
它将表格发送到:
[HttpPost]
public ActionResult WeeklyMonitoring(FormCollection form)
{
string week = form["Weeks"];
ViewBag.Weeks = week;
return View();
}
我得到明显的跟随错误:
无法将'string'类型转换为'System.Collections.Generic.List'
如何避免此错误,并在发送表单后将所选周保留在DropDownList
中?
编辑:
到目前为止,我已经找到了这个解决方案,对于过滤器可能不实用,但在这里我去了:
[HttpPost]
public ActionResult WeeklyMonitoring(FormCollection form)
{
string week = form["Weeks"];
ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));
List<string> wl = pr.FetchWeeks(DateTime.Now.Year);
wl.Remove(week);
wl.Insert(0, week);
wl.Select(m => new SelectListItem { Text = m, Value = m });
ViewBag.Weeks = wl;
return View();
}
我从列表中删除该项并将其置于顶部,因此它将由DDList显示。
EDIT2:
添加FetchWeeks
:
public List<string> FetchWeeks(int year)
{
List<string> weeks = new List<string>();
DateTime startDate = new DateTime(year, 1, 1);
startDate = startDate.AddDays(1 - (int)startDate.DayOfWeek);
DateTime endDate = startDate.AddDays(6);
while (startDate.Year < 1 + year)
{
weeks.Add(string.Format("From {0:dd/MM/yyyy} to {1:dd/MM/yyyy}", startDate, endDate));
startDate = startDate.AddDays(7);
endDate = endDate.AddDays(7);
}
return weeks;
}
EDIT3:感谢kikyalex我们找到了解决方案:
当ViewBag属性与DDList同名时,会产生阻止其更改其值的问题。
答案 0 :(得分:1)
Array
(
[0] => Array
(
[Promo] => P11757
[OfferId] => 412
[Offer Name] => Flipkart
)
[1] => Array
(
[Promo] => P16508
[OfferId] => 1236
[Offer Name] => Flipkart
)
[2] => Array
(
[Promo] => P17087
[OfferId] => 647
[Offer Name] => MakeMyTrip
)
[3] => Array
(
[Promo] => P17088
[OfferId] => 44
[Offer Name] => Makemytrip
)
[4] => Array
(
[Promo] => P17089
[OfferId] => 44
[Offer Name] => Makemytrip
)
[5] => Array
(
[Promo] => P17426
[OfferId] => 140
[Offer Name] => Trendin
)
[6] => Array
(
[Promo] => P18022
[OfferId] => 779
[Offer Name] => Expedia
)
[7] => Array
(
[Promo] => P18442
[OfferId] => 647
[Offer Name] => MakeMyTrip
)
[8] => Array
(
[Promo] => P18443
[OfferId] => 647
[Offer Name] => MakeMyTrip
)
您从下拉列表中获取当前周,并再次将其传递给视图。然后你把它投成 string week = form["Weeks"];
ViewBag.Weeks = week;
。发现错误是正常的。
您应该使用List<string>
SelectListItem
在您的视图中,将广告投放更改为public ActionResult WeeklyMonitoring()
{
ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));
List<string> wl = pr.FetchWeeks(DateTime.Now.Year); // Creates the list
ViewBag.Weeks = wl.Select(m => new SelectListItem { Text = m, Value = m }).ToList();
return View();
}
[HttpPost]
public ActionResult WeeklyMonitoring(FormCollection form)
{
string week = form["Weeks"];
ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));
List<string> wl = pr.FetchWeeks(DateTime.Now.Year);
ViewBag.Weeks = wl.Select(m => new SelectListItem { Text = m, Value = m, Selected = m == week }).ToList();
return View();
}
而不是List<SelectListItem>
List<string>