Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Ajax Post en Asp.net Core en .Net 6 no funciona en el método de acción

Estoy haciendo una llamada ajax en cshtml como a continuación:

 $(document).ready(function(){ $('.dl-dir-list').click(function(e){ console.log($(e.target).data('path')); console.log(JSON.stringify({path: $(e.target).data('path')})); $.ajax({ type: "POST", url: '@Url.Action("GetFiles")', data: JSON.stringify({path: $(e.target).data('path')}), dataType: "json", contentType: "application/json; charset=utf-8", success: function (response) { console.log(response); }, error: function () { alert("Error while getting files"); } }); }); });

Método de acción:

 [HttpPost] public JsonResult GetFiles([FromBody]string path) { return Json(_fileService.GetFilesFromDirectory(path)); }

El problema siempre es que el parámetro de ruta es nulo. ¿Cual podría ser el problema? Esto está en Asp.Net COre, versión .Net 6

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

1.Asegúrese de que $(e.target).data('path') contenga valor.

2. Cambia tu código a:

 data: JSON.stringify($(e.target).data('path')),

Toda la demostración de trabajo:

Vista:

 //be sure add this `data-path` attribute <input type="button" value="Create" class="dl-dir-list" data-path="aaa"/> @section Scripts { <script> $(document).ready(function(){ $('.dl-dir-list').click(function(e){ console.log($(e.target).data('path')) //result: "aaa" $.ajax({ type: "POST", url: '@Url.Action("GetFiles")', data: JSON.stringify($(e.target).data('path')), //change here.... dataType: "json", contentType: "application/json; charset=utf-8", success: function (response) { console.log(response); }, error: function () { alert("Error while getting files"); } }); }); }); </script> }

Controlador:

 [HttpPost] public JsonResult GetFiles([FromBody]string path) { return Json(_fileService.GetFilesFromDirectory(path)); }
over 4 years ago · Santiago Trujillo Report

0

Pruébelo como a continuación,

 data: JSON.stringify({"path": $(e.target).data('path')}),
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!