2013-02-14 17 views
0

這是我的問題的一部分,我刪除,因爲它太寬
我爲我的博客存檔創建了一個ActionLink,但我遇到了麻煩,因爲我打電話給它的多個項目。

這是我的代碼,其返回的錯誤信息沒有重載方法的ActionLink需要7個參數在ActionLink中使用多個項目的正確方法?

@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) 

} 

這是我原來的代碼,但沒有一個鏈接,只給出了一個列表

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

輸出:

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



以下是我在控制器中的操作方式,但我知道它無法正常工作。 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); 


我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; 
     } 

什麼是調用視圖多個項目的正確方法是什麼?

我在這裏嘗試的是查看特定月份和年份下的帖子列表或類似的博客檔案。

最新(工作)
最後我能夠使它發揮作用,這是現在一個工作一個

更新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; 
     } 

更新控制器

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); 
     } 

更新V IEW

@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) 

} 
+1

能你澄清你的意思是「打電話」多個項目?你可以在你的控制器中有'Html.ActionLink(item.AchiveMonth,「Archive」,「Item」,new {AchiveYear = item.AchiveMonth,ArchiveMonth = item.AchiveYear,PostCount = item.PostCount},null)''具有AchiveYear,ArchiveMonth,PostCount作爲參數。 – 2013-02-14 05:01:13

+0

抱歉,我指出的是三項。 ArchiveYear,ArchiveMonth和PostCount ..:D我更新了我的問題,以便您更好地理解。我知道這可以用你的建議來完成,但我不知道該怎麼做。 :(你能幫我糾正一下我的代碼嗎?謝謝.. – bot 2013-02-14 05:22:51

+1

你應該只使用一個鏈接文字給ActionLink。 – 2013-02-14 05:49:30

回答

2

它仍然不是很清楚,我當你說「叫多件物品」,所以這裏是我的答案,假設你想的months years (postcount)的列表到鏈接。要獲得如下

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

此輸出,您可以在連接字符串item.AchiveMonthitem.AchiveYearitem.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 
} 
+0

感謝您的幫助,現在我會將您的建議標記爲答案,但我仍然需要測試它:)。是的,我想在點擊的月份/年份顯示帖子。我嘗試過,但我很難做到這一點,因爲月/年在帖子表T_T內。對不起,我不擅長查詢。如果你願意,我將不勝感激。謝謝。 – bot 2013-02-18 02:01:32

+0

謝謝老兄,我現在能夠使它工作。 :) – bot 2013-02-19 02:23:45

相關問題