這是我的問題的一部分,我刪除,因爲它太寬
我爲我的博客存檔創建了一個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)
}
能你澄清你的意思是「打電話」多個項目?你可以在你的控制器中有'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
抱歉,我指出的是三項。 ArchiveYear,ArchiveMonth和PostCount ..:D我更新了我的問題,以便您更好地理解。我知道這可以用你的建議來完成,但我不知道該怎麼做。 :(你能幫我糾正一下我的代碼嗎?謝謝.. – bot 2013-02-14 05:22:51
你應該只使用一個鏈接文字給ActionLink。 – 2013-02-14 05:49:30