Home > Back-end >  Parameters are coming null from ajax call
Parameters are coming null from ajax call

Time:10-04

I have a following method in ASP.NET MVC:

public PartialViewResult SaveUpdateVariants(string TitleEn, string TitleAr, int ItemID, List<ItemVariant> data)
{
}

I am calling it from ajax call like this

   $.ajax({
        url: '/Inventory/SaveUpdateVariants',
        contentType: 'application/json; charset=utf-8', // Include ContentType
        data : JSON.stringify(update),
        method: "POST",
        type: "POST",
        success: result => {
            gotoOptions();
            $("#variant-table  tbody").replaceWith(result);
            window['variant-table'].refresh();
        }
    })

Update object is like this

{
"TitleEn": "aa",
"TitleAr": "aa",
"ItemID": "829",
"data": [
    {
        "ItemVariantID": "-1",
        "NameEn": "s",
        "NameAr": "a",
        "Amount": "10"
    }
]
} 

But my action method receiving all the parameter as null.

CodePudding user response:

since you are using 'application/json; charset=utf-8' ContentType you have to add FromBody attribute to action and create in c# the same model as you have in java script

public PartialViewResult SaveUpdateVariants([FromBody] Item item)
{
.........
}

public class Item
{
 public string TitleEn {get; set;} 
  ....

 public List<ItemVariant> data {get;set;}

}

CodePudding user response:

Can you try this

data : {TitleEn : update.TitleEn, TitleAr : update.TitleAr, ItemID : update.ItemID}

and so on..

  • Related