简体   繁体   中英

Parameters are coming null from ajax call

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.

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;}

}

Can you try this

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

and so on..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM