简体   繁体   中英

Web Api Post method is receiving NULL parameter always

I am trying to pass List as a parameter to web Api , Using below code;

Client Side

 public async Task<ActionResult>BatchUpdatePartial(MVCxGridViewBatchUpdateValues<NewWorkItem, int> batchValues)
    {

        var updatedItems = new List<NewWorkItem>();

        string url = "http://localhost:9198/api/values";

        foreach (var item in batchValues.Update)
        {
            if (batchValues.IsValid((item)))
            {
                var updatedVals = new NewWorkItem();

                updatedVals.CPK_ID = item.CPK_ID;
                updatedVals.BYR_ID = item.BYR_ID;
                updatedVals.P_ID = item.P_ID;
                updatedVals.CPK_PRI_FLG = item.CPK_PRI_FLG;
                updatedItems.Add(updatedVals);
            }

            else
                batchValues.SetErrorText(item, "Correct Vallidation Errors");
        }

        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.Encoding = System.Text.Encoding.UTF8;
            string serialisedData = JsonConvert.SerializeObject(updatedItems);
            string response = client.UploadString(url, serialisedData);
            Object result = JsonConvert.DeserializeObject(response);
         }

        return PartialView("_GridViewPartial", NewWorkItem.GridData);
    }

Server Side

  public string Post([FromBody]string[] values)
        {
        string seperator = ",";
        string data = string.Join(seperator, values.ToList<string>());
        string result = string.Format("Succesfully uploaded: {0}", data);
        return result;
        }

But I am always getting NULL inside the values at server side ?

Can you please suggest me solution ?

Thanks

Unfortunately, you are not actually sending a string[] the way your POST method expects. You are sending serializedData , which is, by your own definition, a serialization of updatedItems . updatedItems is a list of a reference type - you do not provide the definition of it here, but I guarantee you it is not going to serialize the same way a string does.

You will need to change updatedItems to be List<string> or something similar.

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