1

This is my controller. I have an issue where I am unsure why my Input string is always null. Hope someone can take a look at it and see what my mistake is.

var Name;
$("#AccountCodeName").change(function() {
  Name = $("#AccountCodeName").val();
});

var form_data = {
  "Input": Name,
};

$("#AccountCodeName").autocomplete({
  source: function(request, response) {
    $.ajax({
      url: "/Configuration/AccountCodes/GetAllByName",
      method: "POST",
      data: form_data,
      contentType: "application/json",
      success: function(result) {
        console.log(result)
      }
    });
  }
});
[HttpPost]
public JsonResult GetAllByName([FromBody]string Input)
{
  JsonResult result = new JsonResult(null);
  result = this.Json(new
  {
      list = accountCodeData.GetAllByName(Input),
  });
  return result;
}
6
  • Name is not initialized anywhere? Commented Mar 5, 2019 at 14:03
  • Sorry, just added it. Commented Mar 5, 2019 at 14:04
  • Try without [FromBody] Commented Mar 5, 2019 at 14:11
  • Tried that also. Commented Mar 5, 2019 at 14:14
  • What plugin are you using for .autocomplete ? Commented Mar 5, 2019 at 15:41

3 Answers 3

1

The issue is because you only ever set the form_data.Name when the page loads. It is never updated. Note that it's not a reference value.

To fix the issues you need to instead create the object you provide to data just before you send the AJAX request, like this:

$("#AccountCodeName").autocomplete({
  source: function(request, response) {
    $.ajax({
      url: "/Configuration/AccountCodes/GetAllByName",
      method: "POST",
      data: { 
        Input: $("#AccountCodeName").val()
      },
      contentType: "application/json",
      success: function(result) {
        console.log(result);
        // note that you need to call 'response()' here providing the received data
      }
    });
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

If you put console.log($("#AccountCodeName").val()); in the source handler function, what do you get?
I am able to get the AccountCodeNames value if I put a console.log there. Somehow its not being sent across to my controller
Try data: JSON.stringify({ Input: $("#AccountCodeName").val() }),
Still null. I tried it with both [FromBody] and without [FromBody]. Both had null
0

I finally found an answer

This is how its supposed to be for autocomplete

Controller

    [HttpPost]
    public ActionResult GetAllByName(string term)
    {
        JsonResult result = new JsonResult(null);
        result = this.Json(new
        {
            list = accountCodeData.GetAllByName(term),
        });
        return result;
    }

Jquery

 $("#AccountCodeName").autocomplete({
        source: function (request, response) {
            console.log($("#AccountCodeName").val())
            console.log(request.term)
            $.ajax({
                url: "/Configuration/AccountCodes/GetAllByName",
                method: "POST",
                data: request,
                dataType: 'json',
                success: function (result) {
                    console.log(result)
            }
        });
        }
    });

Comments

0

I have been using jquery post with .net core post action methods for even models

Here is code snippet that may help you

jquery

$.ajax({
    type: "POST",
    url: '/controller/action',
    data: { Name: $('#name').val()},
    success: function (response) {
        window.location.href = '/';
    }
});

.net core back-end

[HttpPost, Route("controller/action")]
public async Task<IActionResult> action(string Name)
{

}

Comments

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.