Skip to content
Loading
  
  • }  
  •   
  • 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
  • 0
    Try below code
     
    Create partial view
     
    public ActionResult Create()
    {
    var model = new MyModel();
    if (Request.IsAjaxRequest())
    {
    return PartialView( "_Partial", model.PartialModel );
    }
    else
    {
    return View( model );
    }
    }
     
    bind the partial view inside the view
     
    $(document).ready(function(){
    setInterval(callajax,1000);
    function callajax()
    {
    $.ajax({
    url: '/controller/create',
    dataType: 'html',
    success: function(data) {
    $('#myPartialContainer').html(data);
    }
    });
    }
    });
    0
  • Salman Beg
    0
    Hey Mark. If you will not use database then it's difficult to manage the value because Everytime you are updating the value. I can understand that you are thinking about every time hitting to db means it will take some time but as we are using asynchronously then performance will not affect much. Thanks.
    0
  • 0
    Salman Beg
     can you check the below code it works fine as i have one view where i have buttons for increment ,decrement and reset and it is working fine but i did not use ajax i normally calling controller method on button click so how to conver the entire stuff to be ajax based 
    0
  • Salman Beg
    0
    Mark. We need to change this code. In the button click event we need to call the action method. You can follow up with this link for example, https://www.c-sharpcorner.com/UploadFile/302f8f/Asp-Net-mvc-using-jquery-ajax/
    0
  • 0
    salman beg what would be data in my case 
     
    0
  • Salman Beg
    0
    I can able to see in your action method that you are passing counter. So from Ajax call also you need to pass counter. Thanks.
    0