1

I have the button on my page:

<input type="button" class="btn btn-default" value="BMW" id="sendBMW"/> 

I try to send Post request to controller using Jquery:

<script type="text/javascript">
    $("#sendBMW").button().click(function () { 
        $.post('/Home/GetCarsByCriteria', 'bmw');
    })
</script>

Here my controller:

[HttpPost]
public IEnumerable<Car> GetCarsByCriteria(string param) {
   var cars = db.Cars.Where(x=> x.Seria == param).ToList();
   return cars;
}

enter image description here

I tried to add [FromBody] attribute in to Controller, but brek-point doesn't work. Does somebody to know how i can solve it ?

1 Answer 1

1

You need to send the data to the controller as a key/value pair, not a string. The simplest way to do that is to give jQuery an object which it will encode for you. Try this:

$("#sendBMW").button().click(function () { 
  $.post('/Home/GetCarsByCriteria', { param: 'bmw' });
})

Note that the key of the object, param in this case, needs to match the parameter of the Action in your controller.

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

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.