Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 473993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:14:33+00:00 2026-05-13T00:14:33+00:00

So I have a simple voting feature on my asp.net mvc page. My index

  • 0

So I have a simple voting feature on my asp.net mvc page. My index page is loaded with all the posts in the database. A user can vote yes or no as to whether they liked the post or not via links within each post. So in my database I have a table called posts and there are two fields in that table named vote_yes and vote_no that track the vote counts.

My vote action has no view because it is a JSONResult method, but my view for the index action looks like this:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<INDA.Models.Post>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Welcome
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <% foreach (var item in Model) { %>

        <div class="post">
            <div class="postText">
                <%= Html.Encode(item.message) %>
            </div>
            <div class="postInfo">
                <span class="timestamp left">
                    Posted by <em><%= Html.Encode(item.author)%></em> on 
                    <%= Html.Encode(String.Format("{0:g}", item.date)) %>
                </span> 
                <span class="voting right">
                    Comments<em>
                        <%= Html.ActionLink(
                                "Liked This (" + Html.Encode(item.vote_yes) + ")",
                                "Vote",
                                new { id = item.post_id, vote = "yes" },
                                new { @class = "vote_yes" } )%>
                    </em> - 
                    <em>
                        <%= Html.ActionLink(
                                "Hated It (" + Html.Encode(item.vote_no) + ")",
                                "Vote",
                                new { id = item.post_id, vote = "no" },
                                new { @class = "vote_no" } )%>
                    </em>
                </span>
                <div class="clear"></div>
            </div>  
        </div>

    <% } %>

</asp:Content>

Right now I’m only testing voting yes, so the jQuery in my master page looks like this:

$(document).ready(function() {
                $(".vote_yes").click(function() {
                    var currLink = $(this);
                    var action = currLink.attr("href");
                    $.getJSON(action, null, function(data) {
                        currLink.html("Liked This (" + data.vote_yes + ")");
                    });
                    return false;
                });
            });

And my PostsController with index and vote actions looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using INDA.Models;

namespace INDA.Controllers
{
    public class PostsController : Controller
    {
        INDARepository INDARepository = new INDARepository();

        // action methods
        [OutputCache(Location = OutputCacheLocation.None)]
        public ActionResult Index()
        {
            var posts = INDARepository.GetAllPosts().ToList();
            return View(posts);
        }

        [OutputCache(Location=OutputCacheLocation.None)]
        public JsonResult Vote(int id, string vote)
        {
            Post p = INDARepository.GetPost(id);
            if (vote == "yes")
            {
                p.vote_yes++;
            }
            else if (vote == "no")
            {
                p.vote_no++;
            }

            INDARepository.Save();

            return Json(p);
        }

    }
}

Now here’s the problem. When I click the “Liked This (X)” link to vote yes in each post, it will go through the AJAX call and update the count in the database fine. However, the link in the first post will never update to reflect the new count of votes in the database, aka it should look like “Liked This (X + 1)” but remains “Liked This (X)”. The strange part is that in all of the other posts, the links update fine. It’s just the first post that never works. Can anyone help? I tried adding the no caching aspect, but to no avail.

Thanks and sorry this is so long,
Ryan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:14:34+00:00Added an answer on May 13, 2026 at 12:14 am

    As far as I can tell there are 3 things which are possibly going wrong here.

    1. Put a breakpoint on the server where you return the post to make sure that you are returning an updated post
    2. Make sure the Json is working correctly – change the line that says currLink.html(“Liked This (” + data.vote_yes + “)”); to something like currLink.html(“bob”) and do an alert(data.vote_yes)
    3. Make sure currLink is pointing to the correct DOM element either by using Firebug as suggested or doing a simple alert again.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple ASP.NET MVC action like this : public ActionResult Edit(EditPostViewModel data) {
I have am trying to create a simple voting page. There are a few
I have a simple voting webpage, where user is presented with a table with
I have simple database - one table with 6 collumns. 3 of them i
I added a simple voting feature to my website. For some reason, the ajax
I have a simple voting system with a colorbox as the voting form. When
I have a simple page with a list of items. I am allowing users
I have a very simple voting form. It contains a single text field for
Let's say i have a simple table voting with columns id(primaryKey),token(int),candidate(int),rank(int). I want to
I have a simple poll site. I would like to disallow possibility of voting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.