skip to Main Content

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.

2

Answers


  1. Can you try this

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

    and so on..

    Login or Signup to reply.
  2. 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;}
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search