@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); } }
source share