MVC Submit List via AJAX

Ok, I saw a lot of questions asked on this question, but none of the answers actually worked for me, here is my AJAX:

$.ajax({ url: "/FilterSessions/GetFilterSession", type: "GET", dataType: "json", data: jsonFilters, traditional: true, success: function (response) { //Haha, it never entering here. not really. } }); 

var "jsonFilters" contains an array with the following data:

 [0] = { Path: "Test", Name: "More testing", Value: "Test Value" }, [1] = { Path: "Test", Name: "More testing", Value: "Test Value" } 

And this is my controller:

 public ActionResult GetFilterSession(List<FilterSessionModel> jsonFilters) { //Do things return Json(false, JsonRequestBehavior.AllowGet); } 

jsonFilters always stays null ... I also tried to add contentType: "application/json; charset=utf-8" to the AJAX call ... but it really didn't do anything

Finally, the FilterSessionModel class FilterSessionModel structured as follows:

  public class FilterSessionModel { public string Path { get; set; } public string Name { get; set; } public string Value { get; set; } } 

Any ideas on what I might lose or what might happen?

Things I've tried so far:

Setting "traditional: true", setting "contentType", using JSON.stringify and trying to accept a string in MVC (no-go)

UPDATE: Thanks to the answer below, I realized that the missing one was to send data using the param id like this:

  data: "{param1ID:"+ param1Val+"}" 
+4
source share
5 answers

I think what you are looking for answers here:

Passing a list of objects to an MVC controller using jQuery Ajax

0
source

I would try to disable the type of your action.

 List<FilterSessionModel> 

Pretty sure that the above will not work, I would try something like Object.

Or perhaps a line in which I will use newton json dll to enter into your list of classes.

The problem boils down to the fact that your action is unable to determine the type, assuming that you validate your data before calling ajax.

** Update due to additional information. Add to the part of the error and view these variations upon returning from your controller, also start the violinist and see what you get for the http-numbers.

 $.ajax({ type: "POST", url: "Servicename.asmx/DoSomeCalculation", data: "{param1ID:"+ param1Val+"}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { UseReturnedData(msg.d); }, error: function(x, t, m, b) { //Look at the vars above to see what is in them. } }); 
+1
source

javascript or ajax call never dial an object.,. You need to set the parameter type of the controller side to either string or List else, which you can also set to type Object. If you changed the code this way .. Your code definitely works !!!

+1
source

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) { //Do things return Json(false, JsonRequestBehavior.AllowGet); } 
0
source
 $.ajax({ url: "/FilterSessions/GetFilterSession", type: "GET", dataType: "json", data:JSON.stringify({ 'jsonFilters': jsonFilters}), contentType: 'application/json; charset=utf-8', success: function (response) { //Do your action } }); 
0
source

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


All Articles