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

336
Views
No se pueden pasar parámetros de JQuery AJAX al controlador

Estoy llamando a un método en JQuery como este

 var data = { 'minAge': minAge, 'MaxAge': maxAge, 'ProductType': ProductType, 'ProductSubject': ProductSubject, 'ordering': '@Ordering.NotOrder', 'Searchkey': "", 'CatId': @Context.Request.Query["CatId"] } $.ajax({ contentType: 'application/x-www-form-urlencoded', dataType: 'json', type: "POST", url: '/ApplyFilter', data: data, success: function (data) { // something happens } })

en controlador

 [HttpPost] public ViewComponentResult ApplyFilter(InvokeRequest invokerequest) { return ViewComponent("GetProducts",invokerequest); } public class InvokeRequest { public Ordering ordering { get; set; } public string Searchkey { get; set; } public string CatId { get; set; } public int MinAge { get; set; } public int MaxAge { get; set; } public int ProductType { get; set; } public int ProductSubject { get; set; } }

pero cuando llamo a JQuery, la solicitud de invocación siempre está vacía, pero en la vista JavaScript se completa correctamente, parece que los parámetros no pueden pasar al controlador

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Es probable que su solicitud de publicación no envíe datos correctamente: veo que le está diciendo a ajax que codifique datos de dos maneras diferentes: contentType: 'application/x-www-form-urlencoded' y dataType: 'json', debe usar uno u otro y si le dices a json que deberías json.stringify(datos) aquí: datos: datos,

también puedes probar tu controlador usando curl.

over 4 years ago · Santiago Trujillo Report

0

Tengo una prueba en mi lado y funcionó para mí. Pruebo en un proyecto asp.net core 5 mvc. Y creé la misma InvokeRequest que la tuya.

 @{ ViewData["Title"] = "Home Page"; } <button id="btn1">test</button> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script> var data = { 'minAge': 1, 'MaxAge': 10, 'ProductType': 0, 'ProductSubject': 0, 'Searchkey': "", 'CatId': "id1" } $("#btn1").click(function(){ $.ajax({ url:'https://localhost:44323/home/sendData', data:data, //dataType: 'json',//if we define the dataType, you need to make sure the response is a json type: "POST", success:function(da){ alert(da); } }) }) </script> //my controller [HttpPost] public string sendData(InvokeRequest req) { return "success"; } //if keep using `dataType: 'json',` then use follow action [HttpPost] public JsonResult sendData(InvokeRequest req) { var a = req.CatId; return Json(a); }

ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo Report

0

Si está utilizando ASP.NET Core
Simplemente agregue '[FromForm]'

 public ViewComponentResult ApplyFilter([FromForm] InvokeRequest invokerequest)

Imagen del resultado aquí



Clase FromFormAttribute

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.fromformattribute?view=aspnetcore-6.0

Enlace de modelos en ASP.NET Core

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0

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!