将整数数组传递给webapi方法

时间:2022-06-14 05:28:19

I am trying to pass an array of int but I can not get the value in the webapi method

我试图传递一个int数组但我无法获得webapi方法中的值

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 :

在apiController中:

[HttpDelete]
        public bool DeleteModel(int[] deletedIds)
        {
            return modelsRepository.DeleteModels(deletedIds);
        }

3 个解决方案

#1


8  

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.

你的代码看起来很漂亮。请定义“deletedIds”对象的结构。一个建议是使用new Array()对象初始化deletedIds属性并删除JSON.stringify()。这里也有类似的问题。

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,

Web API支持以各种方式解析内容数据,但它不处理多个发布的内容值。您的问题的解决方案可能是创建一个具有int []类型属性的ViewModel。像下面的代码,

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.

并将其用作参数类型。

#2


2  

At last, based on @Shashank Answer it worked and the code modified as :

最后,基于@Shashank答案它的工作原理和代码修改为:

var deletedIds = new Array();

deletedIds.push(modelId);

var postData = { "DeletedIds": deletedIds };
$.ajax({
    type: "Delete",
    traditional: true,
    dataType: 'json',
    cache: false,
    url: "/api/Management/Models",
    data: postData,
    success: ModelDeleted,
    error: ModelNotDeleted
});

and the apiController :

和apiController:

[HttpDelete]
        public bool DeleteModels(DeleteViewModel dvm)
        {
            return modelsRepository.DeleteModels(dvm.DeletedIds);
        }

and for the DeleteViewModel :

并为DeleteViewModel:

public class DeleteViewModel
    {
        public int[] DeletedIds { get; set; }
    }

#3


0  

I suspect that the stringify bit is messing up your object - have you tried assigning it to a variable to see if it's the expected formatting?

我怀疑stringify位搞砸了你的对象 - 你是否尝试将它分配给变量以查看它是否是预期的格式?

You're stringifying the whole thing at the moment:

你现在正在整理整个事情:

{ "deletedIds": deletedIds }

{“deletedIds”:deletedIds}

while what you probably want to send as post body is this:

而您可能希望发送的帖子是:

{ "deletedIds": JSON.stringify(deletedIds) }

{“deletedIds”:JSON.stringify(deletedIds)}

#1


8  

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.

你的代码看起来很漂亮。请定义“deletedIds”对象的结构。一个建议是使用new Array()对象初始化deletedIds属性并删除JSON.stringify()。这里也有类似的问题。

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,

Web API支持以各种方式解析内容数据,但它不处理多个发布的内容值。您的问题的解决方案可能是创建一个具有int []类型属性的ViewModel。像下面的代码,

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.

并将其用作参数类型。

#2


2  

At last, based on @Shashank Answer it worked and the code modified as :

最后,基于@Shashank答案它的工作原理和代码修改为:

var deletedIds = new Array();

deletedIds.push(modelId);

var postData = { "DeletedIds": deletedIds };
$.ajax({
    type: "Delete",
    traditional: true,
    dataType: 'json',
    cache: false,
    url: "/api/Management/Models",
    data: postData,
    success: ModelDeleted,
    error: ModelNotDeleted
});

and the apiController :

和apiController:

[HttpDelete]
        public bool DeleteModels(DeleteViewModel dvm)
        {
            return modelsRepository.DeleteModels(dvm.DeletedIds);
        }

and for the DeleteViewModel :

并为DeleteViewModel:

public class DeleteViewModel
    {
        public int[] DeletedIds { get; set; }
    }

#3


0  

I suspect that the stringify bit is messing up your object - have you tried assigning it to a variable to see if it's the expected formatting?

我怀疑stringify位搞砸了你的对象 - 你是否尝试将它分配给变量以查看它是否是预期的格式?

You're stringifying the whole thing at the moment:

你现在正在整理整个事情:

{ "deletedIds": deletedIds }

{“deletedIds”:deletedIds}

while what you probably want to send as post body is this:

而您可能希望发送的帖子是:

{ "deletedIds": JSON.stringify(deletedIds) }

{“deletedIds”:JSON.stringify(deletedIds)}