0
每個條目計數我有一個用戶表和社區表。用戶可以加入多個社區,社區可以擁有多個用戶。所以我有一個名爲CommunityUser表的關係表。 CommunityUser表具有UserID和CommunityID定義哪個用戶註冊在哪個社區。 我想表明我的搜索視圖是 This is somewhat same as Facebook Groups檢索針對一個表中從某些其他表在單個視圖中MVC
我展示社區標誌,標題和細節從社區表與@foreach循環,但我還想添加計數來自CommuntyUser表的每個communityID。 這裏是我的社區模型類
public class Community
{
[Key]
public int CommunityID { get; set; }
[Required]
public string CommunityName { get; set; }
public string CommunityAbout { get; set; }
public string CommunityLogo { get; set; }
public int PrivacyID { get; set; }
//I am trying to get members count using this
public virtual IEnumerable<CommunityUser> CommunityUsers { get; set; }
}
這裏是我的控制器函數,其中我申請這個
public ActionResult ShowSearchResults(string SearchQuery)
{
var communitySearch = from m in db.Communities
select m;
if (!String.IsNullOrEmpty(SearchQuery))
{
communitySearch = communitySearch.Where(s => s.CommunityName.Contains(SearchQuery));
}
return View(communitySearch);
}
這是我的看法類。我已經刪除了額外的代碼,並提到我需要從CommunityUser Table中計數。
@foreach (var item in Model)
{
<div class="row">
<div class="col-md-4">
@Html.DisplayFor(modelItem => item.CommunityName)
<br>
@ViewBag.Count
<!--Need to count of members in that community-->
<!--means How many times that communityID appeared in CommunityUser table-->
<br>
@Html.DisplayFor(modelItem => item.CommunityAbout)
</div>
</div>
}
如何在我的視圖類中訪問此計數列表? –
現在,不要傳遞舊模型,只需傳遞此自定義模型,就可以獲得社區用戶數。 – Naveen