传递整数数组的WebAPI方法 [英] Passing array of integers to webapi Method
问题描述
我想传递一个int数组,但我不能让中的WebAPI方法的value
VAR POSTDATA = {deletedIds:deletedIds}; $阿贾克斯({
输入:删除,
传统:真实,
数据类型:JSON,
的contentType:应用/ JSON,
缓存:假的,
网址:/ API /管理/模型,
数据:JSON.stringify(POSTDATA)
成功:ModelDeleted,
错误:ModelNotDeleted
});
和在apiController:
[HttpDelete]
公共BOOL DeleteModel(INT [] deletedIds)
{
返回modelsRepository.DeleteModels(deletedIds);
}
您code寻找pretty好了给我。请界定deletedIds对象的结构。
一个建议是使用
新的Array()
对象初始化deletedIds财产和删除JSON.stringify()。
类似的问题问<一个href=\"http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form\">here.
修改
网络API支持以多种方式分析内容数据,但它不处理多个发布的内容的值。 a。对于问题的解决办法是创建使用int []类型的属性一个ViewModel。
像code以下,
公共类SimpleViewModel
{
公众诠释[] {deletedIds获得;组; }
}//控制器
[HttpDelete]
公共BOOL DeleteModel(SimpleViewModel deletedIds)
{
返回modelsRepository.DeleteModels(deletedIds.deletedIds);
}
和使用它作为参数类型。
I am trying to pass an array of int but I can not get the value in the webapi method
var postData = { "deletedIds": deletedIds };
$.ajax({
type: "DELETE",
traditional: true,
dataType: 'json',
contentType: 'application/json',
cache: false,
url: "/api/Management/Models",
data: JSON.stringify(postData),
success: ModelDeleted,
error: ModelNotDeleted
});
and in apiController :
[HttpDelete]
public bool DeleteModel(int[] deletedIds)
{
return modelsRepository.DeleteModels(deletedIds);
}
Your code looking pretty Ok to me. Please define structure of "deletedIds" object. one suggestion is to Use new Array() object to initialize deletedIds property and remove JSON.stringify() . A similar question asked here.
EDIT
Web API supports parsing content data in a variety of ways, but it does not deal with multiple posted content values. A solution for your problem could be to create a ViewModel with a property of int[] type. Like code below,
public class SimpleViewModel
{
public int[] deletedIds{ get; set; }
}
//Controller
[HttpDelete]
public bool DeleteModel(SimpleViewModel deletedIds)
{
return modelsRepository.DeleteModels(deletedIds.deletedIds);
}
and use it as parameter type.
这篇关于传递整数数组的WebAPI方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!