title: Partial view based on a query from Data Base
body: <p>I am showing search results same as searching groups on facebook. </p>
  
  <p><img src="https://i.stack.imgur.com/7iUxJ.jpg" alt="Check showing results here"></p>
  
  <p>I have a relationship Table named <code>CommunityUser</code> in Database having attributes <code>CommunityID</code> and <code>UserID</code> .</p>
  
  <p>Using Partial View I want to show if <code>User</code> not already joined that Community/Group that it will show <strong>Join Button</strong> else if user already joined that community it will show <strong>Leave button</strong>.</p>
  
  <p>Complicated step for me is these results are showing with foreach loop. Here is my <strong>Partial View Class</strong> code. I am considering that I am passing query that will return me 1 if that communityID exist against that userID in CommunityUser table else return 0.</p>
  
  <pre><code>@model IEnumerable&lt;FYP.Models.CommunityUser&gt;
  
  @ViewBag.UserID = db.Users.Where(u=&gt; u.UserName == User.Identity.Name).SingleOrDefault();
  @ViewBag.Count = (from CommunityUser in FYP.Models where CommunityUser.Id = UserID &amp;&amp; CommunityUser.CommunityID = ViewBag.ComID select CommunityUser).Count();
  
  @if (ViewBag.Count)
  {
      &lt;button&gt;@Html.ActionLink("Leave", "LeaveCommunity", new { id = ViewBag.ComID })&lt;/button&gt;
  }
  else
  {
      &lt;button&gt;@Html.ActionLink("Join", "joinCommunity", new { id = ViewBag.ComID })&lt;/button&gt;
  }
  </code></pre>