1
视图模型为allSundaysInMonth,我一直在这个相同:创建一个视图模型为的IEnumerable <SelectListItem> allSundaysInMonth
namespace TradeUK.Admin.Web.ViewModels
{
public class SundaysInMonthViewModel
{
public IEnumerable<SelectListItem> AllSundays { set; get; }
public string SelectedSunday { set; get; }
}
}
然后在我的行动的新代码:
public ActionResult TradeUKKPISearchesData() //show dropdownlist in the view
{
var now = DateTime.Now;
var lastMonth = now.AddMonths(-1);
var lastMonthSundays = GetDatesOfSundays(lastMonth.Year, lastMonth.Month);
var thisMonthSundays = GetDatesOfSundays(now.Year, now.Month);
var sundaysToTakeFromLastMonth = 2;
var sundays = lastMonthSundays.Skip(Math.Max(0, lastMonthSundays.Count() - sundaysToTakeFromLastMonth)).Take(sundaysToTakeFromLastMonth).Concat(thisMonthSundays);
var allSundaysInThisMonth = new SundaysInMonthViewModel
{
AllSundays = sundays.Select(x => new SelectListItem
{
Value = x.ToString("dd-MM-yyyy"),
Text = x.ToString("dd-MM-yyyy"),
});
//or must I have the code here? not sure how all this works yet
SelectedSunday = sundays.Select(y => new SelectListItem
{
Value =
Text =
})
};
//this is what I meant or can I add this to the above?
var selectedSunday = new SundaysInMonthViewModel{
SelectedSunday = sundays.Select(y => new SelectListItem
{
Value =
})
}
return View(allSundaysInThisMonth);
}
感谢
谢谢你,我会通过这方面的工作,并尝试和理解。只有一件事:我有System.Globalization命名空间,但CultureInfo的InvariantCulture部分无法识别? – 2012-08-15 08:04:04
我不知道,'CultureInfo.InvariantCulture'就在那里:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx对我来说很好。 – 2012-08-15 08:10:46
代码是好的我犯了一个错字我的坏,我跑了代码,但得到一个错误:传入字典的模型项目类型为'TradeUK.Admin.Web.ViewModels.SundaysInMonthViewModel',但这个字典需要一个模型项目类型'System.Collections.Generic.IEnumerable'1 [System.Collections.Generic.KeyValuePair'2 [System.String,System.Int32]]'?我在视图模型中的类型是IEnumerable AllSundays和string? –
2012-08-15 08:52:46