Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

asp.net mvc - Passing array of integers to webapi Method

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);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...