2

这是我的行动:

public ActionResult List()
        {
            var currentDate = DateTime.Now;
            var list = new List<ArchiveViewModel>();
            for (var startDate = new DateTime(2012, 11, 1); startDate <= currentDate; startDate = startDate.AddMonths(1))
            {
                list.Add(new ArchiveViewModel
                {
                    Month = startDate.Month,
                    Year = startDate.Year,
                    FormattedDate = startDate.ToString("MMMM, yyyy")
                });
            }
            return View("List",list);
        }

这是视图

@model IList<Blog.Web.UI.ViewModels.ArchiveViewModel>

    @foreach (var item in Model)
    {

            @Html.ActionLink(item.FormattedDate, "Post", "Archive", new { month = item.Month, year = item.Year }, null)

    }

这个动作结果的输出是这样的

November, 2012
December, 2012
January, 2013
February, 2013
March, 2013
April, 2013
May, 2013
June, 2013

但我想输出这样的东西

  June, 2013
   May, 2013
  April, 2013
 March, 2013
...
November, 2012

我怎样才能做到这一点 ??

4

3 回答 3

2
var newList = list.Reverse(); //This will change the order of all records

或者

var newList = list.OrderByDescending(x => x.Year)
                  .ThenBy(x => x.Month)
                  .ToList();
于 2013-06-19T21:48:23.677 回答
1

您正在向后创建列表。您可以按照其他人描述的任何一种方式对其进行排序,或者您可以第一次就正确地进行排序。

public ActionResult List()
        {
            var currentDate = DateTime.Now;
            var list = new List<ArchiveViewModel>();
            for (var startDate = currentDate; startDate >= new DateTime(2012, 11, 1); startDate = startDate.AddMonths(-1))
            {
                list.Add(new ArchiveViewModel
                {
                    Month = startDate.Month,
                    Year = startDate.Year,
                    FormattedDate = startDate.ToString("MMMM, yyyy")
                });
            }
            return View("List",list);
        }
于 2013-06-19T21:52:17.897 回答
0

var newList = list.Reverse();

于 2013-06-19T21:49:23.433 回答