Skip to content
Loading
One Page with Counter + - in ASP.NET  
  • class="row" style="text-align:center">  
  • "submit" value="+" class="w3-button w3-block w3-black" />  
  • "submit" value="-" class="w3-button w3-block w3-black" />  
  • @Html.ActionLink("Next", "UpdateCounter", "Home", null, new { @class = "btn btn-primary" })  
  • @Html.ActionLink("Previous", "DecreaseCounter", "Home", null, new { @class = "btn btn-primary" })  
  • @Html.ActionLink("Reset", "ResetCounter", "Home", null, new { @class = "btn btn-primary" })  
  •   
  •   
  • }  
  • Controller Method:
    1. [HttpGet]  
    2. public ActionResult UpdateCounter(Counter counter)  
    3. {  
    4. using (CounterAppForANTHEntities1 entities = new CounterAppForANTHEntities1())  
    5. {  
    6. Counter updatedCustomer = (from c in entities.Counters  
    7. where c.id == 1  
    8. select c).FirstOrDefault();  
    9. updatedCustomer.Counter_value = updatedCustomer.Counter_value+1;  
    10. entities.SaveChanges();  
    11. // return View(updatedCustomer);  
    12. }  
    13. return RedirectToAction("Index");  
    14. }  
    15. [HttpGet]  
    16. public ActionResult DecreaseCounter(Counter counter)  
    17. {  
    18. using (CounterAppForANTHEntities1 entities = new CounterAppForANTHEntities1())  
    19. {  
    20. Counter updatedCustomer = (from c in entities.Counters  
    21. where c.id == 1  
    22. select c).FirstOrDefault();  
    23. updatedCustomer.Counter_value = updatedCustomer.Counter_value - 1;  
    24. entities.SaveChanges();  
    25. // return View(updatedCustomer);  
    26. }  
    27. return RedirectToAction("Index");  
    28. }  
    29. [HttpGet]  
    30. public ActionResult ResetCounter(Counter counter)  
    31. {  
    32. using (CounterAppForANTHEntities1 entities = new CounterAppForANTHEntities1())  
    33. {  
    34. Counter updatedCustomer = (from c in entities.Counters  
    35. where c.id == 1  
    36. select c).FirstOrDefault();  
    37. updatedCustomer.Counter_value = 0;  
    38. entities.SaveChanges();  
    39. // return View(updatedCustomer);  
    40. }  
    41. return RedirectToAction("Index");  
    42. }  
    how i can convert this into the Ajax based methodology and update aschronously , I also need another page where i will haev no buttons i just need to show the counter value which getting update after every seconds .
    0
  • 1. Use setInterval to call ajax method
     
    setInterval(function, milliseconds);
     
    set to function executes on every 1minute and fetches new data from the server.
     
    1. Use UpdatePanel with traget ID and display the value
    2. Programatically trigger button click event inside the update panel and display the result
    0
  • what if i do not use the database? can i achieve the same result 

    Salman Beg

    0
  • Salman Beg
    When you change value from 78 to 79, and after that if four person opened the page then they can able to see 79. But if the page is opened from a long time then they cann't be able to see 79. To resolve this issue i have 2 solutions exists.
    i/ They need to refresh the page.
    ii/ In that page, you can add an ajax call with async:true and need to add timer(after what time that value will be updated automatically). so in the ajax after particular time crossed the ajax call will call automatically and it will hit to the db, get the value and update the particular counter value in that page.as it will work asynchronously so it won't effect that much on performance.
     
    Thanks. 
    0
  • Salman Beg Thanks if i have opened the counter application in three places and I have updated the counter from 78 to 79, Should the other four person who has open up the same page would be able to see the updated value or they need to refresh the page.
    0
  • Salman Beg
    Hi.
    Create a method which will save the counter value in db. Whenever the user clicks call the method using ajax with async:true. So that the method will call asynchronously and it will update the value accordingly. Thanks.
    0