MVC model binds an array of strings from jQuery message

The string[] orderTypeNames approaches zero.

mvc action

 public PartialViewResult EditMultipleOrderStates( string[] orderTypeNames, int[] orderIds) 

javascript

 $('#edit-mulitple-order-states-button').click(function () { ids = []; types = []; $checked = $('.order-queue-order input:checked'); $orders = $checked.closest('.order-queue-order'); $orders.each(function (index, elem) { $order = $(elem); ids.push($order.attr("orderId")); types.push($order.attr("orderType")); }); data = { orderIds: ids, orderTypeNames: types }; $.post('EditMultipleOrderStates', data, function (response) { //... }); }); 

insole

enter image description here

 orderIds%5B%5D=97&orderIds%5B%5D=98&orderIds%5B%5D=93&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder 

Are these the square brackets causing the problem? How can I bind these arrays?

Edit: I manually create a query string in average time.

 query = ""; for each... query += "orderIds=" + $order.attr("orderId") + "&"; query += "orderTypeNames=" + $order.attr("orderType") + "&"; 
+6
source share
3 answers

You need to call some kind of JSON.stringify for the data.

  $.post('EditMultipleOrderStates', JSON.stringify(data), function (response) { //... }); 
+3
source

@Benjamin. Here is a slightly more detailed example of what I did, which works

 <script type="text/javascript"> $(document).ready(function() { $('#btnTest').click(function() { var filter = { OrgId: 3, Drivers: [{ Name: "SGT-200" }, { Name: "SGT-100"}], DrivenUnits: [{ Name: "Generator" }], Regions: [{ Name: "REU" }], SearchString : "Test search string" }; $.ajax( { url: "/api/Dashboard/PostFilter", type: "POST", contentType: "application/json", dataType: "json", data: JSON.stringify(filter), success: function(result) { alert(result); }, error: function(error) { alert("There was an error posting the data to the server: " + error.responseText); } }); }); }); </script> 

Hope this helps.

EDIT: And here are the relevant C # objects

 public class Filter { public int OrgId { get; set; } public List<FilterData> Drivers { get; set; } public List<FilterData> DrivenUnits { get; set; } public List<FilterData> Regions { get; set; } public List<FilterData> Operations { get; set; } public List<FilterData> Connections { get; set; } public string SearchString { get; set; } } public class FilterData { public string Type { get; set; } public string Name { get; set; } public int Count { get; set; } } 

All your type names must match. Below is the code that receives the call

 public IQueryable<DashboardData> Post(Filter filter) { using(_unitOfWork) { _dashboardRepository.UsingUnitOfWork(_unitOfWork); return FilterDashboardData(_dashboardRepository.GetDashboardData(filter.OrgId), filter); } } 
+1
source

There is a good article on passing multiple parameters through POST.

POST Multiple Parameters

This speaks of WebApi, but in this case is still relevant.

0
source

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


All Articles