这是我删除的问题的一部分,因为它太宽泛了
我为我的博客存档创建了一个ActionLink,但我遇到了麻烦,因为我在其中调用了多个项目。
这是我的代码返回错误消息方法没有重载ActionLink需要7个参数
@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);
}
更新了视图
@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)
}
答案 0 :(得分:2)
当你说“拨打多个项目”时,我仍然不清楚,所以这是我的答案,假设您想将months years (postcount)
列表放入链接中。要获得此输出
January 2013 (1)
February 2013 (1)
December 2012 (4)
您可以连接字符串item.AchiveMonth
,item.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
}