我有一个调用web api的MVC应用程序来传递一些数据。
MVC编码
[AcceptVerbs(HttpVerbs.Post)]
public async System.Threading.Tasks.Task<ActionResult> StudentViewModels_Create([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<StudentViewModel> studentviewmodels)
{
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
string baseUrl = "http://localhost:51514/api/Students";
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
JArray postData = new JArray();
//postData.Add(JsonConvert.SerializeObject(request));
foreach(StudentViewModel student in studentviewmodels)
{
postData.Add(JsonConvert.SerializeObject(student));
}
// call the web-api and pass the data
System.Net.Http.HttpResponseMessage response = await client.PostAsync(baseUrl, new StringContent(new JavaScriptSerializer().Serialize(postData), Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
}
}
// this will return new data
//return Json("");
}
这里是发送 postData 变量之前的数据的图片
但是,当调用到达Api时,Json数组为空。
网络-API
public IHttpActionResult Students_Create(JArray inputData)
{
var students = new List<Student>();
for (int i = 1; i<inputData.Count; i++)
{
var tempStudent = new Student();
tempStudent = (Student)JsonConvert.DeserializeObject(inputData[i].ToString());
students.Add(tempStudent);
}
// Will keep the inserted entitites here. Used to return the result later.
var entities = students;
if (ModelState.IsValid)
{
using (var northwind = new StudentEntities())
{
foreach (var student in students)
{
// Create a new Student entity and set its properties from the posted StudentViewModel.
var entity = new Student
{
Studentid = student.Studentid,
Firstname = student.Firstname,
Lastname = student.Lastname,
Age = student.Age
};
// Add the entity.
northwind.Students.Add(entity);
// Store the entity for later use.
entities.Add(entity);
}
// Insert the entities in the database.
northwind.SaveChanges();
}
}
return Json(db.Students);
}
inputData 变量的图片
关于为什么会发生这种情况的任何想法?谢谢你的帮助。
答案 0 :(得分:0)
不是答案,而是对您提供的代码的更多观察。您似乎无法在Students_Create
中的任何位置调用StudentViewModels_Create
方法。这导致我相信您将StudentViewModels_Create
的返回结果传递给Students_Create
,这将无效,因为StudentViewModels_Create
返回一个空的Json对象(return Json("")
)。
答案 1 :(得分:0)
问题在于Json数组。 MVC代码部分
中的这段代码 JArray postData = new JArray();
//postData.Add(JsonConvert.SerializeObject(request));
foreach(StudentViewModel student in studentviewmodels)
{
postData.Add(JsonConvert.SerializeObject(student));
}
当我通过序列化器传递数组时
var serializer = new JavaScriptSerializer();
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(postData);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
// pass the data to the api
System.Net.Http.HttpResponseMessage response = await client.PostAsync(baseUrl, stringContent);
json varaible只包含一个空数组,所以传递给api的是一个空数组。因此我为什么只得到一个空阵列。
我决定尝试使用ArrayList而不是JArray,然后将它传递给序列化程序并且它有效。我不是100%肯定为什么这解决了这个问题,我仍在研究具体原因。
ArrayList postData = new ArrayList();
postData.Add(JsonConvert.SerializeObject(request));
foreach (StudentViewModel student in studentviewmodels)
{
postData.Add(JsonConvert.SerializeObject(student));
}