title: Like button in MVC using 'Ajax.Beginform()' not able to display total likes on page
body: <p>I am beginner in MVC and Ajax development and want a like button in my web, which should work like this: if the user clicks on it total likes will be incremented by <code>1</code> and if the user clicks it again (dislike) then it will decremented by <code>1</code>. What I have done so far is this:</p>
  
  <p>Here's the Model: </p>
  
  <pre><code>public class Like
  {
      public int Id { get; set; }
      public virtual Video Video { get; set; }
      public int VideoID { get; set; }
      public string UserId { get; set; }
      public virtual ApplicationUser ApplicationUser { get; set; }
  }
  </code></pre>
  
  <p>Here is the Controller:
  Post Method</p>
  
  <pre><code>[HttpPost]
      public ActionResult Like(int Id, Like like)
          {
              if (ModelState.IsValid &amp;&amp; User.Identity.IsAuthenticated == true)
              {
                  like.Video = storeDB.Videos.Find(Id);
                  like.UserId = User.Identity.GetUserId();
  
                  var userlike = storeDB.Likes.Where(l =&gt; l.UserId == like.UserId &amp;&amp; l.VideoID == Id);
  
                  if (userlike.Count() == 0)
                  {
                      storeDB.Likes.Add(like);
                      storeDB.SaveChanges();
                  }
                  else if (userlike.Count() == 1)
                  {
                      var likeDel = storeDB.Likes.FirstOrDefault(l =&gt; l.UserId == like.UserId &amp;&amp; l.VideoID == Id);
                      storeDB.Likes.Remove(likeDel);
                      storeDB.SaveChanges();
                  }
                  List&lt;Like&gt; videoLikes = storeDB.Likes.Where(v =&gt; v.VideoID == Id).ToList();
                  int nooflikes = videoLikes.Count();
                  ViewBag.noLikes = nooflikes;
                  return Json(ViewBag.noLikes, JsonRequestBehavior.AllowGet);
              }
              else
              {
                  ViewBag.Message = "Login to like this video";
                  return PartialView("Like", ViewBag.noLikes);
              }
  </code></pre>
  
  <p>This is the Get method of Like:</p>
  
  <pre><code>public ActionResult Like(int id)
      {
          List&lt;Like&gt; videoLikes = storeDB.Likes.Where(v =&gt; v.VideoID == id).ToList();
          int nooflikes = videoLikes.Count();
          ViewBag.noLikes = nooflikes;
          return Json(ViewBag.noLikes, JsonRequestBehavior.AllowGet);
      }
  </code></pre>
  
  <p>and I have created a Partial View for this:</p>
  
  <pre><code>    @if (ViewBag.Message != null)
  {
      &lt;script&gt;
          $(document).ready(function () {
              alert('@ViewBag.Message');
          });
  
      &lt;/script&gt;
  }
  //to display total likes
          &lt;input type="text" id="likes" name="likes" value='@ViewBag.noLikes' readonly="readonly" style="border:none; background-color:transparent; width:20px"  /&gt;&lt;span style="color:black"&gt;likes&lt;/span&gt;
  </code></pre>
  
  <p>and this is the main view in which I am using Ajax.BeginForm()</p>
  
  <pre><code> @using (Ajax.BeginForm("Like", "VOD", new { Id = Model.Id },
               new AjaxOptions
              {
                  HttpMethod = "Post",
                  InsertionMode = InsertionMode.Replace,
                  UpdateTargetId = "likecount"
              }))
                          {
  
                              &lt;button type="submit" id="like" value="Like" class=" btn btn-primary btn-xs"&gt; Like &amp;nbsp; &lt;span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"&gt;&lt;/span&gt; &lt;/button&gt;
                          }
                          &lt;span id="likecount"&gt;
                              @Html.Partial("Like", new Models.Like())
                          &lt;/span&gt;
  </code></pre>
  
  <p>Now the issue is that when the page is first loaded it is not displaying total likes, and when I click on like button it returns <code>1</code> and when I click like button again it returns <code>0</code> but when I refresh the page or when I post back to another page total likes again disappear.</p>
  
  <p>Can anybody help me?</p>