大家好我在jquery中构建一个json对象并试图将它发布到我的api但是数据没有发布那里有任何人可以帮我解决这个问题
this is my json object
{"Employee":[{"EmployeeCode":"1","EmployeeName":"Sudheer","EmployeeId":"1"}]}
我的模特
public class Employee
{
public long EmployeeId{ get; set; }
public long EmployeeCode{ get; set; }
public string EmployeeName{ get; set; }
}
我的api方法
[ActionName("InsertOrUpdateEmployee")]
public void InsertOrUpdateEmployee(List<Employee> Employee)
{
new repo.InsertOrUpdateEmployee(Employee);
}
调用API的JavaScript代码
PreReqisites.Employee=[];//in this i push my employee details
var employees= JSON.stringify(PreReqisites.Employee);
var employeedata= '{"Employee":' + jsdCustomers + '}';
var varemployee= jQuery.parseJSON(employeedata);
console.log(varemployee);
alert(varemployee);
alert(JSON.stringify(varemployee));
$.post("api/Employee/InsertOrUpdateEmployee", { Employee: varemployee}, function (data) {
alert("success");
});
当提出一个断点并检查它我的员工数量为零时,数据没有发布那里有任何人可以帮我这个
答案 0 :(得分:0)
[HTTPPost]无法正常工作,我正在对其进行故障排除,但你可以使用[HTTPGet]执行相同的数据发布,它正在按预期工作。
发布数据的Javascript代码
var postData = { "Employee": [{ "EmployeeCode": "1", "EmployeeName": "Sudheer", "EmployeeId": "1"}] };
$.ajax({
type: "GET",
url: "api/Employee/EmployeeController",
data: postData,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (data) {
//alert(data.Result);
}
});
控制器操作方法
public class EmployeeController : ApiController
{
[HttpGet]
public void InsertOrUpdateEmployee([FromUri]IList<Employee> Employee)
{
}
}
显示校样的调试视图的屏幕截图