RobertoCarlos Melgar

RobertoCarlos Melgar

  • 1.6k
  • 159
  • 10.2k

load data with ajax and .net core

Dec 15 2020 10:53 AM
good morning, I have the following problem to solve.
I show you the context
 

I have a project with .net core 3.1 and mvc in which by means of scafolding I connect to the database, the models, controllers and views were created, execute the application and effectively everything should be running dome. but actually what I want is to do a crud with jquery and ajax, and that's when I already have a problem, for this I go on to show the code I have.

 mi Controller
  1. public ComAnticipoesController(ChilyContext context)  
  2.         {  
  3.             _context = context;  
  4.         }  
  5.   
  6.         // GET: ComAnticipoes  
  7.         public async Task<IActionResult> Index()  
  8.         {  
  9.             return View(await _context.ComAnticipos.ToListAsync());  
  10.         }  
 a View Index
  1. @model IEnumerable<DBfirts.Models.ComAnticipo>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Index";  
  5. }  
  6.   
  7. <h1>Index</h1>  
  8.   
  9. <p>  
  10.     <a asp-action="Create">Create New</a>  
  11. </p>  
  12. <table id="example" class="table table-striped table-bordered" style="width:100%">  
  13.     <thead>  
  14.         <tr>  
  15.             <th>  
  16.                 @Html.DisplayNameFor(model => model.EmpresaId)  
  17.             </th>  
  18.             <th>  
  19.                 @Html.DisplayNameFor(model => model.TipoAnticipoId)  
  20.             </th>  
  21.             <th>  
  22.                 @Html.DisplayNameFor(model => model.AnticipoNro)  
  23.             </th>  
  24.             <th>  
  25.                 @Html.DisplayNameFor(model => model.Fecha)  
  26.             </th>  
  27.             <th>  
  28.                 @Html.DisplayNameFor(model => model.PedCompraId)  
  29.             </th>  
  30.             <th>  
  31.                 @Html.DisplayNameFor(model => model.ProveedorId)  
  32.             </th>  
  33.             <th>  
  34.                 @Html.DisplayNameFor(model => model.MonedaId)  
  35.             </th>  
  36.             <th>  
  37.                 @Html.DisplayNameFor(model => model.TipoCambio)  
  38.             </th>  
  39.             <th>  
  40.                 @Html.DisplayNameFor(model => model.AnticipoDes)  
  41.             </th>  
  42.             <th>  
  43.                 @Html.DisplayNameFor(model => model.AnticipoObs)  
  44.             </th>  
  45.             <th>  
  46.                 @Html.DisplayNameFor(model => model.MontoAnt)  
  47.             </th>  
  48.             <th>  
  49.                 @Html.DisplayNameFor(model => model.MontoDes)  
  50.             </th>  
  51.             <th>  
  52.                 @Html.DisplayNameFor(model => model.GestionId)  
  53.             </th>  
  54.             <th>  
  55.                 @Html.DisplayNameFor(model => model.TipoCompId)  
  56.             </th>  
  57.             <th>  
  58.                 @Html.DisplayNameFor(model => model.CompNro)  
  59.             </th>  
  60.             <th>  
  61.                 @Html.DisplayNameFor(model => model.PlanId)  
  62.             </th>  
  63.             <th>  
  64.                 @Html.DisplayNameFor(model => model.EstadoId)  
  65.             </th>  
  66.             <th>  
  67.                 @Html.DisplayNameFor(model => model.SLastUpdateId)  
  68.             </th>  
  69.             <th>  
  70.                 @Html.DisplayNameFor(model => model.DtLastUpdateDt)  
  71.             </th>  
  72.             <th>  
  73.                 @Html.DisplayNameFor(model => model.IConcurrencyId)  
  74.             </th>  
  75.             <th>  
  76.                 @Html.DisplayNameFor(model => model.ConFac)  
  77.             </th>  
  78.             <th>  
  79.                 @Html.DisplayNameFor(model => model.SinFac)  
  80.             </th>  
  81.             <th>  
  82.                 @Html.DisplayNameFor(model => model.CentroCostoId)  
  83.             </th>  
  84.             <th>  
  85.                 @Html.DisplayNameFor(model => model.AnticipoOrden)  
  86.             </th>  
  87.             <th>  
  88.                 @Html.DisplayNameFor(model => model.AnticipoIdOrg)  
  89.             </th>  
  90.             <th></th>  
  91.         </tr>  
  92.     </thead>  
  93.     <tbody>  
  94.         @foreach (var item in Model)  
  95.         {  
  96.             <tr>  
  97.                 <td>  
  98.                     @Html.DisplayFor(modelItem => item.EmpresaId)  
  99.                 </td>  
  100.                 <td>  
  101.                     @Html.DisplayFor(modelItem => item.TipoAnticipoId)  
  102.                 </td>  
  103.                 <td>  
  104.                     @Html.DisplayFor(modelItem => item.AnticipoNro)  
  105.                 </td>  
  106.                 <td>  
  107.                     @Html.DisplayFor(modelItem => item.Fecha)  
  108.                 </td>  
  109.                 <td>  
  110.                     @Html.DisplayFor(modelItem => item.PedCompraId)  
  111.                 </td>  
  112.                 <td>  
  113.                     @Html.DisplayFor(modelItem => item.ProveedorId)  
  114.                 </td>  
  115.                 <td>  
  116.                     @Html.DisplayFor(modelItem => item.MonedaId)  
  117.                 </td>  
  118.                 <td>  
  119.                     @Html.DisplayFor(modelItem => item.TipoCambio)  
  120.                 </td>  
  121.                 <td>  
  122.                     @Html.DisplayFor(modelItem => item.AnticipoDes)  
  123.                 </td>  
  124.                 <td>  
  125.                     @Html.DisplayFor(modelItem => item.AnticipoObs)  
  126.                 </td>  
  127.                 <td>  
  128.                     @Html.DisplayFor(modelItem => item.MontoAnt)  
  129.                 </td>  
  130.                 <td>  
  131.                     @Html.DisplayFor(modelItem => item.MontoDes)  
  132.                 </td>  
  133.                 <td>  
  134.                     @Html.DisplayFor(modelItem => item.GestionId)  
  135.                 </td>  
  136.                 <td>  
  137.                     @Html.DisplayFor(modelItem => item.TipoCompId)  
  138.                 </td>  
  139.                 <td>  
  140.                     @Html.DisplayFor(modelItem => item.CompNro)  
  141.                 </td>  
  142.                 <td>  
  143.                     @Html.DisplayFor(modelItem => item.PlanId)  
  144.                 </td>  
  145.                 <td>  
  146.                     @Html.DisplayFor(modelItem => item.EstadoId)  
  147.                 </td>  
  148.                 <td>  
  149.                     @Html.DisplayFor(modelItem => item.SLastUpdateId)  
  150.                 </td>  
  151.                 <td>  
  152.                     @Html.DisplayFor(modelItem => item.DtLastUpdateDt)  
  153.                 </td>  
  154.                 <td>  
  155.                     @Html.DisplayFor(modelItem => item.IConcurrencyId)  
  156.                 </td>  
  157.                 <td>  
  158.                     @Html.DisplayFor(modelItem => item.ConFac)  
  159.                 </td>  
  160.                 <td>  
  161.                     @Html.DisplayFor(modelItem => item.SinFac)  
  162.                 </td>  
  163.                 <td>  
  164.                     @Html.DisplayFor(modelItem => item.CentroCostoId)  
  165.                 </td>  
  166.                 <td>  
  167.                     @Html.DisplayFor(modelItem => item.AnticipoOrden)  
  168.                 </td>  
  169.                 <td>  
  170.                     @Html.DisplayFor(modelItem => item.AnticipoIdOrg)  
  171.                 </td>  
  172.                 <td>  
  173.                     <a asp-action="Edit" asp-route-id="@item.AnticipoId">  
  174.                         <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil" viewBox="0 0 16 16">  
  175.                             <path fill-rule="evenodd" d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5L13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175l-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z" />  
  176.                         </svg>  
  177.                     </a> |  
  178.                     <a asp-action="Details" asp-route-id="@item.AnticipoId">  
  179.                         <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-eye" viewBox="0 0 16 16">  
  180.                             <path fill-rule="evenodd" d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.134 13.134 0 0 0 1.66 2.043C4.12 11.332 5.88 12.5 8 12.5c2.12 0 3.879-1.168 5.168-2.457A13.134 13.134 0 0 0 14.828 8a13.133 13.133 0 0 0-1.66-2.043C11.879 4.668 10.119 3.5 8 3.5c-2.12 0-3.879 1.168-5.168 2.457A13.133 13.133 0 0 0 1.172 8z" />  
  181.                             <path fill-rule="evenodd" d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z" />  
  182.                         </svg>  
  183.                     </a> |  
  184.                     <a asp-action="Delete" asp-route-id="@item.AnticipoId">  
  185.                         <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">  
  186.                             <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z" />  
  187.                             <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4L4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z" />  
  188.                         </svg>  
  189.                     </a>  
  190.                 </td>  
  191.             </tr>  
  192.   
  193.         }  
  194.   
  195.     </tbody>  
  196.   
  197. </table>  
 

In the _Layout.cshtml, I made the following change

  1. <li class="nav-item">  
  2.                             @*<a id="anticipo" class="nav-link text-dark" asp-area="" asp-controller="ComAnticipoes" asp-action="Index">Anticipos</a>*@  
  3.                             <a id="anticipo" class="nav-link text-dark" asp-area="" >Anticipos</a>  
  4.                         </li>  
 
 

and add at the bottom this script

 
  1. <script>  
  2.         $("#anticipo").click(function () {  
  3.             //alert("Hola mundo")  
  4.             CargarDatos();  
  5.         });  
  6.         function CargarDatos() {  
  7.             $.ajax({  
  8.                 type: "Get",  
  9.                 //url: "ComAnticipoes/Index",  
  10.                 url: "@Url.Content("~/ComAnticipoes/Index")",  
  11.                 cache: false,  
  12.                 success: function (dataHtml) {  
  13.                     $("~/#example").html(dataHtml);  
  14.                 }  
  15.             });  
  16.         }  
  17.           
  18.     </script>  

I tried some other options in the url thinking that it might not find the Index sheet or view but I have not managed to load my data.

 

Please help me to solve my problem and see what my mistake was

Thanks you

 
Roberto 
 
 

Answers (2)