Passing array of integers to webapi Method
Solution 1
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.
Solution 2
At last, based on @Shashank Answer it worked and the code modified as :
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 :
[HttpDelete]
public bool DeleteModels(DeleteViewModel dvm)
{
return modelsRepository.DeleteModels(dvm.DeletedIds);
}
and for the DeleteViewModel :
public class DeleteViewModel
{
public int[] DeletedIds { get; set; }
}
Muhammad Alaa
An accomplished individual with a strong background in JavaScript, HTML and CSS. Strongly believes in the power of the Internet, and has an intense desire to learn how to improve the webs core functionality, and to also be involved in its future development. Have a good understand of programming, can come up with plenty of innovative ideas and possesses superb communication skills. Technical skills Microsoft visual studio, Microsoft Team foundation server. ASP.Net Web forms, ASP.Net MVC3, MVC4, AJAX, Linq, JavaScript, jQuery, Json, HTML, XHTML, CSS, XML, Web Services, WPF, WindowsPhone8, WinRT, WinForms, EntityFramework, LinqToSql, Telerik OpenAccess, Android, JSP, JSF, JPA, Primefaces. DBMS: Microsoft SqlServer (2005, 2008, 2012), MySQL5.5, Oracle10g, Microsoft Access.
Updated on June 08, 2022Comments
-
Muhammad Alaa almost 2 years
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); }
-
Muhammad Alaa about 11 yearsI tried to use new Array but still get null on webapi method parameter
-
Muhammad Alaa about 11 yearsThanks for your reply but the question you referenced is not the same my code is working fine when I use action method in regular controller but when url is to webapi it can not get the data
-
Shashank about 11 yearsUse firebug to test post data.
-
Muhammad Alaa about 11 yearsthe data is ok on client side but the problem is webapi uses different model binding than mvc
-
Shashank about 11 yearsdecorating your action parameter with the [FromUri] attribute. this post may help you.
-
Joanna Derks about 11 yearsAnd either way, if your controller method expects integers, why stringify them in JS?
-
Muhammad Alaa about 11 yearsstringify was just a try I made up a test action method in a regular controller and it works mvc modelbinder works but webapi not !!
-
Joanna Derks about 11 years@MuhammadAlaa - post the exact JSON you're sending with the request. Is the controller method hit at all? Are you getting an empty array?
-
Muhammad Alaa about 11 yearsI tried to send static data just for test I get an empty array in the apicontroller method
-
Joanna Derks about 11 years@Muhammad Alaa - post here the JSON you are sending, static or not, otherwise I can't help you