Marcelo Souza

Marcelo Souza

  • NA
  • 25
  • 6.3k

ASP.NET AJAX MVC - Calling Controller Method from View

Dec 16 2017 9:36 AM
Hi
 
I need to call a controller method (this method will return TRUE or FALSE) from my view in order to show a "hands-up or hands-down" image. Bellow you will find my code. Am I in the right path? I don't have experience with ajax so every help is appreciated.
 
The results below are return only one image (the first line in the table), despite of the table on my Index view returning many lines. What am I doing wrong?
 
What should I do to my boolean method return true or false, depending on a parameter (like IdClient)? Note that in this example, I'm always returning false (just to test it...).
 
Controller Method:
 
public bool GetTrueOrFalse()
{
return false;
}
 
My View:
  1. @model IEnumerable<AutoCompleteDropdownlistTeste.Models.Cliente>  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. }  
  5. <h2>Index</h2>  
  6. <script src="~/Scripts/jquery-1.10.2.js"></script>  
  7. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>  
  8. <style>  
  9. .wrapper {  
  10. float: left;  
  11. clear: left;  
  12. display: table;  
  13. table-layout: fixed;  
  14. }  
  15. img.img-responsive {  
  16. display: table-cell;  
  17. max-width: 20%;  
  18. }  
  19. .icon-size {  
  20. font-size: 20px;  
  21. }  
  22. </style>  
  23. <script type="text/jscript">  
  24. $.ajax({  
  25. url: '@Url.Action("GetTrueOrFalse", "Clientes" )',  
  26. success: function (result) {  
  27. var str = result.toLowerCase();  
  28. if (JSON.parse(str)) {  
  29. $("#ddlVerFalse").append("<a class='glyphicon glyphicon-thumbs-up icon-size text-success' href='/membroes/Edit/1' title='Editar'> </a>")  
  30. else {  
  31. $("#ddlVerFalse").append("<a class='glyphicon glyphicon-thumbs-down icon-size text-danger' href='/membroes/Edit/1' title='Editar'> </a>")  
  32. }  
  33. }  
  34. });  
  35. </script>  
  36. <p>  
  37. @Html.ActionLink("Create New""Create")  
  38. </p>  
  39. <table class="table">  
  40. <tr>  
  41. <th>  
  42. Status  
  43. </th>  
  44. <th>  
  45. @Html.DisplayNameFor(model => model.NmCliente)  
  46. </th>  
  47. <th>  
  48. @Html.DisplayNameFor(model => model.Estado.NmEstado)  
  49. </th>  
  50. <th></th>  
  51. </tr>  
  52. @foreach (var item in Model)  
  53. {  
  54. <tr>  
  55. <td>  
  56. <p id="ddlVerFalse"></p>  
  57. </td>  
  58. <td>  
  59. @Html.DisplayFor(modelItem => item.NmCliente)  
  60. </td>  
  61. <td>  
  62. @Html.DisplayFor(modelItem => item.Estado.NmEstado)  
  63. </td>  
  64. <td>  
  65. @Html.ActionLink("Edit""Edit"new { id=item.IdCliente }) |  
  66. @Html.ActionLink("Details""Details"new { id=item.IdCliente }) |  
  67. @Html.ActionLink("Delete""Delete"new { id=item.IdCliente })  
  68. </td>  
  69. </tr>  
  70. }  
  71. </table>  
Thanks.

Answers (4)