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

352
Views
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 answers
Answer question

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 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!