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