2012-08-15 60 views
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); 
} 

感謝

回答

0
public IEnumerable<DateTime> GetDatesOfSundays(int year, int month) 
{ 
    var ci = CultureInfo.InvariantCulture; 
    for (int i = 1; i <= ci.Calendar.GetDaysInMonth(year, month); i++) 
    { 
     var date = new DateTime(year, month, i); 
     if (date.DayOfWeek == DayOfWeek.Sunday) 
     { 
      yield return date; 
     } 
    } 
} 

然後在你的控制器中:

public ActionResult TradeUKKPISearchesData() 
{ 
    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 
     { 
      // TODO: Adjust the formats you want for the 
      // Value and Text of your ddl 
      Value = x.ToString("yyyy-MM-dd"), 
      Text = x.ToString("yyyy-MM-dd"), 
     }), 
     SelectedSunday = thisMonthSundays 
      .Where(x => x < now) 
      .Last() 
      .ToString("yyyy-MM-dd") 
    }; 
    return View(allSundaysInThisMonth); 
} 

,並在您的視圖:

@model SundaysInMonthViewModel 
... 
@Html.DropDownListFor(x => x.SelectedSunday, Model.AllSundays) 
+0

謝謝你,我會通過這方面的工作,並嘗試和理解。只有一件事:我有System.Globalization命名空間,但CultureInfo的InvariantCulture部分無法識別? – 2012-08-15 08:04:04

+0

我不知道,'CultureInfo.InvariantCulture'就在那裏:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx對我來說很好。 – 2012-08-15 08:10:46

+0

代碼是好的我犯了一個錯字我的壞,我跑了代碼,但得到一個錯誤:傳入字典的模型項目類型爲'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

相關問題