[英]Web Api Post method is receiving NULL parameter always
I am trying to pass List as a parameter to web Api , Using below code; 我试图将List作为参数传递给web Api,使用下面的代码;
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 ? 但我总是在服务器端的值内获取NULL?
Can you please suggest me solution ? 你能建议我解决方案吗?
Thanks 谢谢
Unfortunately, you are not actually sending a string[]
the way your POST method expects. 不幸的是,你实际上并没有像POST方法所期望的那样发送
string[]
。 You are sending serializedData
, which is, by your own definition, a serialization of updatedItems
. 您正在发送
serializedData
,它是您自己定义的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. updatedItems
是一个引用类型的列表 - 你不在这里提供它的定义,但我保证你不会像字符串那样序列化。
You will need to change updatedItems
to be List<string>
or something similar. 您需要将
updatedItems
更改为List<string>
或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.