C# asp.net mvc中动态模型视图的创建

C# asp.net mvc中动态模型视图的创建,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我是新的Asp.Net Mvc。我正在为写博客做一个示例应用程序。我试图为档案创建局部视图,根据日期对帖子进行分类 Month/Year(count) Post 1 Post 2 Month/Year(count) Post 1 Post 2 内部控制器 [ChildActionOnly] public ActionResult Archives() { var post = from p in db.Posts

我是新的Asp.Net Mvc。我正在为写博客做一个示例应用程序。我试图为档案创建局部视图,根据日期对帖子进行分类

Month/Year(count)
  Post 1
  Post 2
Month/Year(count)
  Post 1
  Post 2
内部控制器

[ChildActionOnly]
    public ActionResult Archives()
    {
        var post = from p in db.Posts
                   group p by new { Month =p.Date.Month, Year = p.Date.Year } into d
                   select new { Month = d.Key.Month , Year = d.Key.Year , count = d.Key.Count(), PostList = d};

        return PartialView(post);
    }

请帮助我编写此操作的视图,包括本月、本年、本次计数和本次收集的帖子。

您可能正在努力,因为您正在将匿名类型传递到视图中。我将创建一个
ViewModel
来表示您的类型

public class Archive
{
  public string Month { get; set; }
  public string Year { get; set; }
  public int Count { get; set; }
  public ICollection<Post> Posts { get; set; }
}
然后,您可以在
归档
类中强烈键入视图

@model IEnumerable<Archive>

@foreach (Archive archive in Model)
{
  <h2>
    @archive.Month / @archive.Year
  </h2>
}
@model IEnumerable
@foreach(模型中的存档)
{
@archive.Month/@archive.Year
}
如果我能提供进一步的帮助,请告诉我


马特

@否决票我的问题错了吗?
@model IEnumerable<Archive>

@foreach (Archive archive in Model)
{
  <h2>
    @archive.Month / @archive.Year
  </h2>
}