1

I am working in project that I need to use ajax call to save object data from my page witch contain order details. When I use project with .NET framework version 4.5, it is working very well - but when I use ASP.NET Core MVC, it is not working. Any help please?

I did try every thing in similar questions but they did not work. For example I use notation [frombody] - but I still have the same problem.

I have the following controller action:

public ActionResult CreateOrders([FromBody] T_POS_ENT_ORDER_DETIALS_Temp []T_POS_ENT_ORDER_Data)
{
    GRepository<T_POS_ENT_ORDER_DETIALS_Temp> t_pos_ent_orderOpr = new GRepository<T T_POS_ENT_ORDER_DETIALS_Temp>();

    T_POS_ENT_ORDER_Data.order_Date = DateTime.Now;
    t_pos_ent_orderOpr.Add(T_POS_ENT_ORDER_Data);

    return Json(new { msg = "Successfully added " }); 
}

And I have the following ajax call:

    var DATA = [];
    DATA.push({ LocPrice: "12" });
    DATA.push({ LocProductID: "1002" });
    DATA.push({ discount: "0" });
    DATA.push({ posNumber: "1" });
    DATA.push({ productName: "soap" });
    DATA.push({ productQynt: "1" });
    $.ajax({

    url: '/AjaxT_POS_ENT_ORDER/CreateOrders',
    data: JSON.stringify({ 'billArray': DATA }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    type: "POST",
    async: false,

    success: OnSuccess,

  });

I also have this class:

public class T_POS_ENT_ORDER_DETIALS_Temp
{
    public string LocPrice { get; set; }
    public string LocProductID { get; set; }
    public string discount { get; set; }
    public string  posNumber { get; set; }
    public string productName { get; set; }
    public int productQynt { get; set; }
}

There is no error showing only the order details is always null ...

0

3 Answers 3

4

I'm working in .Net Core 3.1 and this works for me:

1. Controller

Try [FromForm] instead [FromBody]

    [HttpPost]
    public ActionResult CreateOrdersPOSTObject([FromForm] List<T_POS_ENT_ORDER_DETIALS_Temp> prm)
    {
        //your logic...
        return Json(new { msg = "Successfully added " });
    }

2. AJAX Call

Neither JSON.stringfy() nor contentType should be used.

            var DATA = [];
            DATA.push({ LocPrice: "12", LocProductID: "1002", discount: "0", posNumber: "1", productName: "soap", productQynt: "1" });
            DATA.push({ LocPrice: "23", LocProductID: "1003", discount: "0", posNumber: "1", productName: "shampoo", productQynt: "1" });
            DATA.push({ LocPrice: "7",LocProductID: "1004" ,discount: "0",posNumber: "2" ,productName: "sponge",productQynt: "2"});

            $.ajax({
                url: '@Url.Content("~/home/CreateOrdersPOSTObject")',
                data: { "prm": DATA },
                type: "POST",
                dataType: "json",
                async: true
            });

Or if you prefer, you can use the shorthand $.post with the same results

            var DATA = [];
            DATA.push({ LocPrice: "12", LocProductID: "1002", discount: "0", posNumber: "1", productName: "soap", productQynt: "1" });
            DATA.push({ LocPrice: "23", LocProductID: "1003", discount: "0", posNumber: "1", productName: "shampoo", productQynt: "1" });
            DATA.push({ LocPrice: "7",LocProductID: "1004" ,discount: "0",posNumber: "2" ,productName: "sponge",productQynt: "2"});

            $.post(
                '@Url.Content("~/home/CreateOrdersPOSTObject")',
                { "prm": DATA },
                function () {
                  alert( "success" );
                })
              .fail(function() {
                alert( "error" );
              });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is the only thing I got to work after trying different solutions for a few hours.
3

You need to change JSON.stringify({ 'billArray': DATA }) to JSON.stringify(DATA) like below:

1.View:

<script>
    $(document).ready(function () {
        var DATA = [];
        DATA.push({ LocPrice: "12" });
        DATA.push({ LocProductID: "1002" });
        DATA.push({ discount: "0" });
        DATA.push({ posNumber: "1" });
        DATA.push({ productName: "soap" });
        DATA.push({ productQynt: "1" });
        $.ajax({
            url: '/AjaxT_POS_ENT_ORDER/CreateOrders',
            data: JSON.stringify(DATA),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            type: "POST",
            async: false
        });
    });
</script>

2.Controller:

[HttpPost]
public ActionResult CreateOrders([FromBody] T_POS_ENT_ORDER_DETIALS_Temp[] T_POS_ENT_ORDER_Data)
{
    //your logic...
    return Json(new { msg = "Successfully added " });
}

3.Result: enter image description here

By the way, if you want to pass them as one array object instead of six arrays,you need to change your DATA like below:

DATA.push({ LocPrice: "12",LocProductID: "1002" ,discount: "0",posNumber: "1" ,productName: "soap",productQynt: "1"});

Comments

0

I do not see an array in your data, there is only a single object with some fields. Also billArray does not match T_POS_ENT_ORDER_Data, and model binding is done by name matching.

To fix this, you should adjust your code to only post and receive a single object.:

public ActionResult CreateOrders(T_POS_ENT_ORDER_DETIALS_Temp T_POS_ENT_ORDER_Data)

...

var DATA = {
    LocPrice: "12",
    LocProductID: "1002",
    discount: "0",
    posNumber: "1",
    productName: "soap",
    productQynt: "1"
 });

$.ajax({
    url: '/AjaxT_POS_ENT_ORDER/CreateOrders',
    data: JSON.stringify(DATA),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    type: "POST",
    async: false,
    success: OnSuccess
});

Minor, there is a typo in class name "DETIALS" -> "DETAILS".

2 Comments

i did every thing you said but still the object is null when i make debug in the controller
Try reducing the passed object to a single string, go right back to basics and build up from there. DATA = "XXX" and in your controller use [HttpPost] CreateOrders(string thing) etc...

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.