MVC Send list through AJAX

前端 未结 5 568
情话喂你
情话喂你 2020-12-19 16:28

Okay, I\'ve seen tons of questions posted regarding this question, but none of the answers has actually worked for me, here\'s my AJAX:

$.ajax({
        url:         


        
相关标签:
5条回答
  • 2020-12-19 16:53

    I would try switching out the type on your action.

    List<FilterSessionModel>
    

    Pretty sure the above is not going to work, I would try something like Object.

    Or possibly a string that I would then use newton json dll to push into your List of Class.

    The problem boils down to your action being unable to figure out the type, assuming you are checking your data prior to the ajax get being called.

    **Update due to more info. Add in the error portion and view those vars on return from your controller, also fire up fiddler and watch what your are getting for 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.
        }
    });
    
    0 讨论(0)
  • 2020-12-19 16:58

    First off I'm making the assumption that your $.ajax is for JQuery and not some other Javascript framework. Please correct me if that's wrong.

    ASP.NET MVC can actually do what you are asking it to (resolve data sent via AJAX to a List<FilterSessionModel>, but it seems to have a difficult time doing it via a GET request. It would help to know which version of ASP.NET MVC you are using, as more is required to get this working on the older versions. However, what I'm suggesting should work on MVC 3 or 4.

    When you send AJAX via JQuery using a GET request and passing it a JavaScript array, this is what you are sending to the server:

    http://localhost:50195/FilterSessions/GetFilterSession?undefined=&undefined=
    

    It's no wonder the model is null because no data is actually being sent.

    I believe ASP.NET can accept objects (and even arrays of objects) like this, but it won't do so with it formatted as JSON (like via JSON.stringify) as that just results in the following request:

    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}]
    

    The way you probably want to do this is with a POST request. ASP.NET MVC will actually accept a JSON string as POST data and will decode it and resolve the model properly. Your AJAX code works fine with a couple 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 posted should recognize POST data already, but in case it doesn't, a simple [HttpPost] attribute is all you need:

    [HttpPost]
    public ActionResult GetFilterSession(List<FilterSessionModel> jsonFilters)
    {
        //Do things
    
        return Json(false, JsonRequestBehavior.AllowGet);
    }
    
    0 讨论(0)
  • 2020-12-19 17:14

    I think what you are looking for is answered here:

    Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

    0 讨论(0)
  • 2020-12-19 17:15
    $.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 讨论(0)
  • 2020-12-19 17:16

    javascript or ajax call never type cast the object. . .you need to set type of the controller side parameter either string or List else you can also set the Object type. . If you modified codein that way.. .Your code definitely work !!!

    0 讨论(0)
提交回复
热议问题