我正在制作我的第一个Asp.net Mvc应用程序-论坛系统。我正在尝试显示子类别中有多少帖子和主题。
这是我的表:
public class Category
{
private ICollection<SubCategory> subCategories;
public Category()
{
this.subCategories = new HashSet<SubCategory>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<SubCategory> SubCategories
{
get { return this.subCategories; }
set { this.subCategories = value; }
}
}
public class SubCategory
{
private ICollection<Thread> threads;
public SubCategory()
{
this.threads = new HashSet<Thread>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Thread> Threads
{
get { return this.threads; }
set { this.threads = value; }
}
}
public class Thread
{
private ICollection<Post> posts;
public Thread()
{
this.posts = new HashSet<Post>();
}
public int Id { get; set; }
public string Title { get; set; }
public virtual SubCategory SubCategory { get; set; }
public int SubCategoryId { get; set; }
public virtual ICollection<Post> Posts
{
get { return this.posts; }
set { this.posts = value; }
}
public string AuthorId { get; set; }
public virtual ApplicationUser Author { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Content { get; set; }
public int ThreadId { get; set; }
public virtual Thread Thread { get; set; }
public string AuthorId { get; set; }
public virtual ApplicationUser Author { get; set; }
}
这是我的看法:
@model IEnumerable<ForumSystem.Web.ViewModels.Home.IndexCategoryViewModel>
@{
ViewBag.Title = "Home Page";
}
<div class="container">
@foreach (var category in Model)
{
<div class="row">
<h5>@category.Name</h5>
@foreach (var subCat in category.SubCategories)
{
<div class="col-md-10">
<div class="row">
<h7>
<a href="/SubCategory/@subCat.Title">@subCat.Title</a>
</h7>
</div>
<div class="row">
<p>@subCat.Description</p>
</div>
</div>
<div class="col-md-2">
<p>@category.ThreadsCount threads</p>
<p>@category.PostsCount posts</p>
<div class="row">
</div>
</div>
}
</div>
}
</div>
@ category.ThreadsCount和PostsCount不起作用。我可以使用@ subCategory.Threads.Count轻松查看线程数,但无法获取帖子数。在控制器中,我尝试了很多事情。目前的代码是:
public ActionResult Index()
{
var threadScount = this.Data
.SubCategories
.All()
.SelectMany(x => x.Threads)
.Count();
var postsCount = this.Data
.Threads
.All()
.SelectMany(x => x.Posts)
.Count();
var model = this.Data
.Categories
.All()
.Select(c => new IndexCategoryViewModel
{
Name = x.Name,
SubCategories = c.SubCategories,
ThreadsCount = threadScount,
PostsCount = postsCount,
})
.ToList();
return this.View(model);
}
但这给了我所有线程和帖子数,而不是每个子类别都特定。提前致谢。
创建表示您要显示的视图的视图模型
public class SubCategoryVM
{
public string Title { get; set; }
public string Description { get; set; }
public int ThreadsCount { get; set; }
public int PostCount { get; set; }
}
public class CategoryVM
{
public string Name { get; set; }
public List<SubCategoryVM> SubCategories { get; set; }
}
控制器
public ActionResult Index()
{
var model = this.Data.Categories.Select(c => new CategoryVM
{
Name = c.Name,
SubCategories = c.SubCategories.Select(s => new SubCategoryVM
{
Title = s.Title,
Description = s.Description,
ThreadsCount = s.Threads.Count,
PostsCount = s.Threads.SelectMany(t => t.Posts).Count;
})
});
return View(model);
}
看法
@model IEnumerable<CategoryVM>
@{
ViewBag.Title = "Home Page";
}
<div class="container">
@foreach (var category in Model)
{
<div class="row">
<h5>@category.Name</h5>
@foreach (var subCategory in category.SubCategories)
{
<div class="col-md-10">
<div class="row">
<h7>
// The following line in your view makes no sense
// <a href="/SubCategory/@subCat.Title">@subCat.Title</a>
// Assuming you have method: public ActionResult Details(string title) in SubCategoryController, then
@Html.ActionLink(subCategory.Title, "Details", "SubCategory", new { title = subCategory.Title }, null)
</h7>
</div>
<div class="row">
<p>@subCategory.Description</p>
</div>
</div>
<div class="col-md-2">
<p><span>@subCategory.ThreadCount</span><span>threads</span></p>
<p><span>@subCategory.PostCount</span><span>posts</span></p>
</div>
}
</div>
}
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句