2

despite I dug into stackoverflow and a lot of documentation, I am stuck on that: I am not able to send an object via jquery ajax to a c# mvc application. Always the values of attributes of the order I return in the controller seems empty.

Thanks for any help!

JAVASCRIPT

function Order()
{
    this.Partner;
    this.OrderID;
    this.TypeOfOrder;
    this.SubmittedBy;
    this.CompanyID;
    this.CompanyName;
}

jQuery('button[type="submit"]').click(function (event) {
    event.preventDefault();

    var order = new Order();
    order.Partner = "ABC",
    order.OrderID = "123";
    order.TypeOfOrder = "Website";

    console.log(order);

    jQuery.ajax({
        url: "/my-route",
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: order,
        success: function (data) {
            console.log(data);
        },
        failure: function (response) {
            console.log("ERROR!");
        }
    });
})

C# Model

public class Order
{
    public string Partner { get; set; }
    public string OrderID { get; set; }
    public string TypeOfOrder { get; set; }
    public string SubmittedBy { get; set; }
    public string CompanyID { get; set; }
    public string CompanyName { get; set; }
}

C# Controller

// POST my-route
[HttpPost]
public Order Post(Order order)
{
    return order;
}

CONSOLE

enter image description here

2
  • 1
    You need to remove contentType: "application/json", (either that or you need to use data: JSON.stringify(order),) Commented May 31, 2018 at 8:51
  • Many thanks for your help, works like a charm now! Commented May 31, 2018 at 20:02

3 Answers 3

1

You need to change your method signature to include [FromBody]

so: public Order Post([FromBody]Order order)

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

1 Comment

Nope, I tried [FromBody] and the result from controller is undefined. I tried also [FromForm] and the result is an object with empty attributes.
0

Just remove content type in ajax call. It will look like this.

jQuery.ajax({
    url: "/my-route",
    type: "POST",
    dataType: 'json',
    data: order,
    success: function (data) {
        console.log(data);
    },
    failure: function (response) {
        console.log("ERROR!");
    }
});

ajax will default set content type.

Comments

0
  • Just remove dataType and contentType from code as given below function.
  • Url should be "/ControllerName/MethodName" i have checked it in my system with controller name and its working fine.

    jQuery.ajax({ url: "/controllerName/methodName", type: "POST", data: order, success: function (data) { console.log(data); }, failure: function (response) { console.log("ERROR!"); } });

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.