How to pass a list of identifiers in an AJAX request to a server in MVC

In an AJAX request on a server in MVC, how can I pass the id list to a controller function?

I accept with or without HTML helpers.

I know that the MVC middleware has no problem when it comes to simple types like int , string and bool .

Is it something like what I should use and massage instead in action?

I don't care if I need to use array or List , and even if the strings I int or strings , I can always convert them. I just need them on the server. My List List is now null.

JavaScript:

 var ids= [1,4,5]; // ajax request with ids.. 

MVC action:

 public ActionResult ShowComputerPackageBuffer(List<int> ids) // ids are null { // build model ect.. return PartialView(model); } 

EDIT: Added my AJAX request

 $(document).ready(function () { $('#spanComputerPackagesBuffer').on('click', function () { var ids = $('#divComputerPackagesBuffer').data('buffer'); console.log('bufferIds: ' + bufferIds); var data = { ids: ids }; var url = getUrlShowComputerPackageBuffer(); loadTable(url, "result", data); }); }); // AJAX's function loadTable(url, updateTargetId, data) { var promise = $.ajax({ url: url, dataType: "html", data: data }) .done(function (result) { $('#' + updateTargetId).html(result); }) .fail(function (jqXhr, textStatus, errorThrown) { var errMsg = textStatus.toUpperCase() + ": " + errorThrown + '. Could not load HTML.'; alert(errMsg); }); }; // URL's function getUrlShowComputerPackageBuffer() { return '@Url.Action("ShowComputerPackageBuffer", "Buffer")'; }; 

SOLUTIONS: // Thanks to @aherrick's comment. I missed the good old "traditional"

 $.ajax({ type: "POST", url: '@Url.Action("ShowComputerPackageBuffer", "Buffer")', dataType: "json", traditional: true, data: { bufferIds: bufferIds } }); 
+5
source share
3 answers

Use the traditional parameter and set it to true .

 $.ajax({ type: "POST", url: "/URL", dataType: "json", traditional: true, data: {} }); 
+10
source

Try this (I checked it):

 $(function () { var ids = [1, 4, 5]; $.ajax({ type: 'POST', contentType: 'application/json; charset=utf-8', url: '@Url.Action("YourAction", "YourController")', data: JSON.stringify( { ids: ids }) }).done(function () { }); }); 

You need to make sure your contentType is application/json and your data is gated.

+2
source
 public ActionResult SaveSomething(int[] requestData) //or public ActionResult SaveSomething(IEnumerable<int> requestData) 

With Action Result, you cannot get a JSON object:

Using the control:

 [HttpPost] [Route( "api/Controller/SaveSomething" )] public object SaveTimeSheet( int[] requestData ) { try { doSomethingWith( requestData ); return new { status = "Ok", message = "Updated!" }; } catch( Exception ex ) { return new { status = "Error", message = ex.Message }; } } 

java script:

 var ids = [1,4,5]; var baseUrl: 'localhost/yourwebsite' $.ajax({ url: baseUrl + '/api/Controller/SaveSomething', type: 'POST', data: JSON.stringify(ids), dataType: 'json', contentType: 'application/json', error: function (xhr) { alert('Error: ' + xhr.statusText); }, success: function (result) { if (result != undefined) { window.location.href = window.location.href; } }, async: false, }); 
+1
source

Source: https://habr.com/ru/post/1214663/


All Articles