Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

c# - MVC Send list through AJAX

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: "/FilterSessions/GetFilterSession",
        type: "GET",
        dataType: "json",
        data: jsonFilters,
        traditional: true,
        success: function (response) {
            //Haha, it's 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 remains null... I have also tried adding contentType: "application/json; charset=utf-8" to the AJAX call... but that didn't really do anything

Finally, the class FilterSessionModel is structured as follows:

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

Any ideas as to what I might be missing or what might be happening?

Things I've tried so far:

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

UPDATE: Thanks to the answer below I realized that what was missing was to send over the data with the param Id like so:

 data: "{param1ID:"+ param1Val+"}"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...