c# - MVC 通过 AJAX 发送列表

标签 c# jquery ajax asp.net-mvc

好的,我已经看到关于这个问题的大量问题,但没有一个答案对我有用,这是我的 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"包含一个包含以下数据的数组:

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

这是我的 Controller :

public ActionResult GetFilterSession(List<FilterSessionModel> jsonFilters)
{
    //Do things

    return Json(false, JsonRequestBehavior.AllowGet);
}

jsonFilters 始终保持为空...我也尝试将 contentType: "application/json; charset=utf-8" 添加到 AJAX 调用中...但这并没有真正做任何事情

最后,FilterSessionModel类的结构如下:

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

关于我可能遗漏了什么或可能发生什么的任何想法?

到目前为止我尝试过的事情:

设置“traditional: true”,设置“contentType”,使用 JSON.stringify 并尝试在 MVC Controller 中接受字符串(禁止)

更新:感谢下面的回答,我意识到缺少的是像这样发送带有参数 ID 的数据:

 data: "{param1ID:"+ param1Val+"}"

最佳答案

我会尝试切换您的操作类型。

List<FilterSessionModel>

很确定上面的方法行不通,我会尝试像 Object.

或者可能是一个字符串,然后我会使用 newton json dll 将其推送到您的类列表中。

问题归结为您的操作无法确定类型,假设您在调用 ajax 之前检查数据。

**因更多信息而更新。添加错误部分并在从 Controller 返回时查看这些变量,同时启动 fiddler 并观察您获得的 http 编号。

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

关于c# - MVC 通过 AJAX 发送列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807302/

相关文章:

c# - Azure Cosmos 数据库集合未获取分区键

c# - 通过 cast 或 Convert.ToSingle() 将 double 转换为 float?

c# - WMI 查询是否允许分组依据?

php - 如何在 jQuery 的同一窗口中加载 PHP 页面

php - 从按钮发送数据到 php 页面并发布结果

php - Ajax 无法在 codeigniter 中工作来填充下拉列表

c# - 数据绑定(bind) WPF 列表框?

jquery - 使用 Bootstrap Switch 将 Check_Box 设置为 nil 默认值

javascript - 如何使用 AJAX 验证 Google Recaptcha V3 响应

java - 只刷新这部分数据(谷歌地图)