First, I make the assumption that your $.ajax
for JQuery
and not some other Javascript structure. Please correct me if it is wrong.
ASP.NET MVC can actually do what you ask (to allow data sent via AJAX to List<FilterSessionModel>
, but it seems like a tricky time to do this with a GET request. This will help you know which version of ASP you are using. NET MVC, since it takes more to work in older versions, however what I suggest should work on MVC 3 or 4.
When you send AJAX via jQuery using a GET request and pass a JavaScript array to it, this is what you send to the server:
http://localhost:50195/FilterSessions/GetFilterSession?undefined=&undefined=
Unsurprisingly, the model is null because in fact no data is sent.
I believe that ASP.NET can accept objects (and even arrays of objects) like this, but this will not be done with its formatting as JSON (for example, via JSON.stringify), as this leads to the following query:
http://localhost:50195/FilterSessions/GetFilterSession?[{%22Path%22:%22Test%22,%22Name%22:%22TestName%22,%22Value%22:%22Testing%22},{%22Path%22:%22Test%22,%22Name%22:%22TestName%22,%22Value%22:%22Testing%22}]
As you probably want to do this, this is a POST request. ASP.NET MVC will actually accept the JSON string as POST data and decrypt it and correctly resolve the model. Your AJAX code works fine with several modifications:
$.ajax({ url: "/FilterSessions/GetFilterSession", type: "POST", //Changed to POST dataType: "json", data: JSON.stringify(jsonFilters), //Pack data in a JSON package. contentType: "application/json; charset=utf-8", //Added so ASP recognized JSON traditional: true, success: function (response) { alert('Success!'); } });
The controller you sent should already recognize the POST data, but if that is not the case, you will need a simple [HttpPost]
attribute:
[HttpPost] public ActionResult GetFilterSession(List<FilterSessionModel> jsonFilters) {