C# 基于数据库查询的局部视图

C# 基于数据库查询的局部视图,c#,.net,linq,model-view-controller,C#,.net,Linq,Model View Controller,我显示的搜索结果与facebook上的搜索组相同 我在数据库中有一个名为CommunityUser的关系表,它具有属性CommunityID和UserID 使用部分视图,我想显示如果用户尚未加入该社区/组,它将显示加入按钮,否则如果用户已加入该社区,它将显示离开按钮 对我来说,复杂的一步是这些结果在foreach循环中显示出来。这是我的局部视图类代码。我正在考虑传递的查询将返回我1,如果该communityID与CommunityUser表中的该用户ID相对,否则返回0 @model IEnu

我显示的搜索结果与facebook上的搜索组相同

我在数据库中有一个名为
CommunityUser
的关系表,它具有属性
CommunityID
UserID

使用部分视图,我想显示如果
用户
尚未加入该社区/组,它将显示加入按钮,否则如果用户已加入该社区,它将显示离开按钮

对我来说,复杂的一步是这些结果在foreach循环中显示出来。这是我的局部视图类代码。我正在考虑传递的查询将返回我1,如果该communityID与CommunityUser表中的该用户ID相对,否则返回0

@model IEnumerable<FYP.Models.CommunityUser>

@ViewBag.UserID = db.Users.Where(u=> u.UserName == User.Identity.Name).SingleOrDefault();
@ViewBag.Count = (from CommunityUser in FYP.Models where CommunityUser.Id = UserID && CommunityUser.CommunityID = ViewBag.ComID select CommunityUser).Count();

@if (ViewBag.Count)
{
    <button>@Html.ActionLink("Leave", "LeaveCommunity", new { id = ViewBag.ComID })</button>
}
else
{
    <button>@Html.ActionLink("Join", "joinCommunity", new { id = ViewBag.ComID })</button>
}
@model IEnumerable
@ViewBag.UserID=db.Users.Where(u=>u.UserName==User.Identity.Name).SingleOrDefault();
@ViewBag.Count=(来自FYP.Models中的CommunityUser,其中CommunityUser.Id=UserID&&CommunityUser.CommunityID=ViewBag.ComID选择CommunityUser)。Count();
@如果(ViewBag.Count)
{
@ActionLink(“离开”,“离开社区”,新的{id=ViewBag.ComID})
}
其他的
{
@ActionLink(“Join”,“joinCommunity”,new{id=ViewBag.ComID})
}

问题出在哪里?局部视图无法处理此查询