1

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 :)

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hello Yong! First of all, thank you so much for your asnwer, it worked fine. But, isn't it a bad pratice to create a entire model class for just a property? And if it isn't, should I face it as a kind of DTO? Hugs from Brazil :) 🇧🇷
Hi, I would say it is more opinion-based. As you post an object to the controller, you need an input parameter with class/object type. So the class created is acted as DTO.
Consider that you are trying to GET data, another approach is to change your controller method as GET such [HttpGet("SearchStudies/{searchString}")]public async Task<IActionResult> SearchStudies(string searchString). So you don't need to create a model class as the controller method will get the value from url. Ya, and with the GET, you need to modify your ajax as well. So (two approaches, GET or POST) it depends on your use case/preference.
0
$.ajax({
    url: "/Studies/SearchStudies/?searchString=" + searchString,
    // ...
})

1 Comment

Please provide more context, code-only answer are not always useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.