Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
ASP.NET MVC do not accept ajax data object on controller

I've been searching for a while and nothing helped me to solve this problem. I don't know what is happening, I used to send information to controller through a data object from AJAX using JQuery, but in this case, the parameter arrive null. My request look like this (I created a function based on button click):

Js Method on Razor page

const searchStudy = () => {
        let searchString = JSON.stringify({"searchString": $("#searchString").val()});
        console.log(searchString)

        $.ajax({
            url: "@Url.Action("SearchStudies","Studies")",
            method: "POST",
            processData: false,
            data: searchString,
            contentType: "application/json; charset=UTF-8",
            dataType: "html",
            success: function(response) {
                console.log(response)
                $("#dataContainer").html(response);
            }
        });
    }

Controller

[HttpPost]
public async Task<IActionResult> SearchStudies(string searchString)
{
    if (String.IsNullOrWhiteSpace(searchString))
        return BadRequest("Search something!");

    var studies = await _studyService.GetSearchStudies(searchString);

    return PartialView("~/Views/Studies/_Studies.cshtml", studies);
}

Ps.: When I try to use query data on url it works. Hope some of you have face this problem once :)

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Create a model class for the expected object to be received.

public class SearchStudiesModel
{
    public string SearchString { get; set; }
}

Change the controller method signature to expect to receive SearchStudiesModel and apply [FromBody] to get the value from the request body.

[HttpPost]
public async Task<IActionResult> SearchStudies([FromBody]SearchStudiesModel model)
{
    if (String.IsNullOrWhiteSpace(model.SearchString))
        return BadRequest("Search something!");

    ...
}

Debugging result

enter image description here

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda