0

This is a part of my question which I deleted because its too broad
I created a ActionLink for my blog Archive but I'm having trouble with it because I'm calling multiple items inside it.

This is my codes which return an error message No overload for method ActionLink takes 7 arguments

@model IEnumerable <Project.Models.ArchiveListModel>

@foreach (var item in Model)
{
<br />
@Html.ActionLink(item.AchiveMonth, item.AchiveYear, item.PostCount, "ArchiveBrowse", "Post", 
new { AchiveYear =  item.AchiveMonth, ArchiveMonth = item.AchiveYear, PostCount = item.PostCount }, null) 

}

This is my original codes but doesn't have a link it only gives a list

@foreach (var item in Model)
{
<br />
<li> @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item.AchiveMonth) @item.AchiveYear (@item.PostCount) </li>   
} 
</fieldset><br/>

output:

January 2013 (1)
February 2013 (1)
December 2012 (4)



Here's how I do it in my controller but I know its not working. T_T

 public ActionResult Archive()
        {
            var archivelst = repository.AchiveList().ToList();
            return View(archivelst);
        }

  //this one below is not working
  public ActionResult ArchiveBrowse(string archive)
    {
        var achivemodel = db.Posts.Include("Posts").Single(a => a.Title == archive);
        return View(achivemodel);
    }
            return View(achivemodel);


My ArchiveRepository

 public IQueryable<ArchiveListModel> AchiveList()
        {

            var ac = from Post in db.Posts
                     group Post by new { Post.DateTime.Year, Post.DateTime.Month }
                     into dategroup
                     select new ArchiveListModel()
                      {

                         AchiveYear = dategroup.Key.Year,
                         AchiveMonth = dategroup.Key.Month,
                         PostCount = dategroup.Count()

                       };

            return ac;
        }

What's the correct way to call multiple items in the view?

What I'm trying here is to view the list of Posts under a specific month and year or something like Blog Archives.

Latest Update(working)
Finally I was able to make it work this is now a working one

Updated ArchiveRepository

 public IQueryable<ArchiveListModel> AchiveList()
        {

            var ac = from Post in db.Posts
                     group Post by new { Post.DateTime.Year, Post.DateTime.Month }
                     into dategroup
                     select new ArchiveListModel()
                      {

                         AchiveYear = dategroup.Key.Year,
                         AchiveMonth = dategroup.Key.Month,
                         PostCount = dategroup.Count()

                       };

            return ac;
        }

Updated Controller

public ActionResult ArchiveBrowse(int AchiveYear, int AchiveMonth, int PostCount)
        {
            var archivemodel = (from a in db.Posts
                                where a.DateTime.Year == AchiveYear &&
                                      a.DateTime.Month == AchiveMonth
                                select a).ToList();


            return View(archivemodel);
        }

Updated View

@foreach (var item in Model)
{
@Html.ActionLink(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item.AchiveMonth) + "" + item.AchiveYear + " (" + item.PostCount + ")",
  "ArchiveBrowse", "Post", new
  {
      AchiveYear = item.AchiveYear,
      AchiveMonth = item.AchiveMonth,
      PostCount = item.PostCount
  }, null) 

}
4

1 回答 1

2

当您说“调用多个项目”时,我仍然不清楚,所以这是我的答案,假设您想将列表months years (postcount)添加到链接中。要在下面获得此输出

January 2013 (1)
February 2013 (1)
December 2012 (4)

您可以连接字符串item.AchiveMonthitem.AchiveYear并且item.PostCount

@Html.ActionLink(item.AchiveMonth + " " + item.AchiveYear + " ("+ item.PostCount + ")",
"ArchiveBrowse", 
"Post", 
    new { 
          AchiveYear =  item.AchiveYear, 
          ArchiveMonth = item.AchiveMonth, 
          PostCount = item.PostCount },
 null)

然后在 中ArchiveBrowse,确保您的参数正确排列:

public ActionResult ArchiveBrowse(string AchiveYear, string ArchiveMonth, string PostCount)
{
     //Not sure if you want to display posts in the clicked month/year
}
于 2013-02-17T12:19:17.940 回答