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 9073417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:24:44+00:00 2026-06-16T18:24:44+00:00

Using MVC 3. (The code below was an attempt to recreate the toggle effect

  • 0

Using MVC 3.
(The code below was an attempt to recreate the toggle effect of stackoverflow’s checked answer)
I have a ajax actionlink when you click it calls an update to db to switch the flag/image. However it works one time, if I click it again it will not toggle. Because there is no postback, model.IsIssue is not updated (its server code). Not sure the best approach to fix the issue. Should I handle it in the code, where I check the current flag in db and pass it back to the view. Or via Jquery, not sure how to code it (my preference)?

My code in the view (toggle):

 <div>
                @Html.Raw(
                    Ajax.ActionLink("[replacethis]", 
                        "ToggleEnabled",
                                 new { questionId = question.QuestionID, reviewId = step.ReviewID, flag = question.IsIssue },
                                 new AjaxOptions { UpdateTargetId = "toggleimage" + question.QuestionID })
                   .ToHtmlString()
                   .Replace("[replacethis]",
                       string.Format("<div id='toggleimage{0}'><img src='/Content/images/{1}' border='0' alt='toggle'/></div>",
                                                                   question.QuestionID, question.IsIssue ? "issue_on.png" : "issue_off.png")
                   )
                )        

My action controller:

public ActionResult ToggleEnabled(int questionId, int reviewId, bool flag)
        {
            using (var db = new NexGenContext())
            {
                db.Database.ExecuteSqlCommand(
                           "EXEC SP_AddUpdateQuestionFlagged @QuestionID, @ReviewID",
                           new SqlParameter("@QuestionID", questionId),
                           new SqlParameter("@ReviewID", reviewId)
                       );
                return flag ? Content("<img src='/Content/images/issue_off.png' border=0  />") : Content("<img src='/Content/images/issue_on.png' border=0  />");

            }
        }
  • 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-06-16T18:24:45+00:00Added an answer on June 16, 2026 at 6:24 pm

    I would just change a css class to toggle the image.

    So instead of holding the image in img … I would use a div and placing the image as a background.

    So, instead of replacing the html with the DIV and IMG in it (what removes the callback to handle the click), just change the class of the div.

    In your view:

    <div id="myimage" class="NormalState"></div>
    

    In JS:

    $('#myimage').click(function () {
         $.ajax({
             url: 'yourControllerActionURL'
             type: 'POST',
             contentType: "application/json; charset=utf-8",
             data: {'questionId': question.QuestionID, 'reviewId': step.ReviewID, 'flag': question.IsIssue },
             success: function (data) {
                if(data.isClicked == true){
                     $('#myimage').addClass('ClickState').removeClass('NormalState');
                }
                else{
                     $('#myimage').addClass('NormalState').removeClass('ClickState');
                } 
             }
         });
    });
    

    In your CSS:

    .NormalState { background-color: #ABCDEF;}
    .ClickState { background-color: #FF0000;}
    

    In your controller:

    You need to return JSON with isClicked set as true or false. You could also return the css class to apply from the controller, but that way your controller knows about the view and that is not the idea of the MVC pattern. So telling the view what happenned more suitable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in mvc 3 razor @using (Html.BeginForm(MyAction, MyController)) { <input type=text
I'm using .net4.5rc with MVC4.0rc The code below is taken from a MVC webapi
I am using MVC 3 with Entities, now I have used the below line
i am using mvc 3 code first. facing problem while passing data form SecurityAttribute
I am using ASP.net MVC with C#. Why this is code: public IQueryable<Product> ListProducts(string
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I'm using Unity with an Asp.net MVC 3 app. Here is some code running
I'm working in ASP.NET MVC 1.0 using MvcContrib FluentHtml version 1.0. The following code
I am developing a ASP.NET MVC 3 application, i am using entity framework code
Im using MVC-Viewmodel, EF Model first on my project. I have a foreach loop

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.