MVC .NET 上的 ajax post 未正确传递数组

2023-12-25

我有一个简单的模式,它使用 select2 从服务器获取产品列表。用户可以选择多个产品,然后点击“确定”来优化搜索。

我的以下设置从模式中获取数据,并使用强类型视图模型对控制器操作执行 ajax 调用,该视图模型与 JS 尝试通过 ajax 调用发送的内容相匹配。

Ajax:

var exploreFilters = {
    "type" : exploreType,
    "products" : $('#s2id_select2-products').select2('data'),
    "locations" : $("#page-report__data").data("criteria__locations"),
    "companies" : $("#page-report__data").data("criteria__companies"),
    "usertypes" : $("#page-report__data").data("criteria__usertypes"),
    "groupusers" : $("#page-report__data").data("criteria__groupusers"),
    "datestart" : $("#page-report__data").data("criteria__datestart"),
    "dateend" : $("#page-report__data").data("criteria__dateend")
};

$.ajax({
    dataType: "html",
    type: "POST",
    url: "/Report/Group/FilteredView",
    data: exploreFilters,
    success: function(html) {
        if($.trim(html) === "")
            $targetSection.html('<div class="page-report__empty">No data found.  Please adjust your search filters and try again.</div>');
        else
            $targetSection.html(html);
    },
    error: function(xhr, text, err) {
        if(text === "timeout")
            $targetSection.html('<div class="page-report__empty">The request timed out.  Please try again.</div>');
        else
            $targetSection.html('<div class="page-report__empty">There has been an error.</div>');
    }
});

Right before the ajax call goes to the controller I inspect the content and structure of exploreFilters: exploreFilters content and structure

以下是 POST 请求中表单数据的外观:

另一方面,我有一个控制器,它采用强类型参数,其结构类似于 exploreFilters 的结构:

public ActionResult FilteredView(ReportCriteriaViewModel criteria)
{
    throw new NotImplementedException();
}

我的强类型视图模型:

public class ReportCriteriaViewModel
{
    public ProductViewModel[] Products { get; set; }
    public string[] Locations { get; set; }
    public string[] Companies { get; set; }
    public string UserTypes { get; set; }
    public string GroupUsers { get; set; }
    public string DateStart { get; set; }
    public string DateEnd { get; set; }
}

public class ProductViewModel
{
    public Guid Id { get; set; }
    public string Text { get; set; }
}

一旦控制器操作被命中,我可以看到 DateStart 和 DateEnd 已成功绑定,但不是我的产品列表。

我无法更改 json 请求的数据类型,它必须是 html,因为我的控制器操作将返回 html。

我尝试更改 Id 和 Text、JSON.stringify 的大小写(这实际上使日期不再绑定)

我究竟做错了什么?


尝试在 Ajax 请求中添加 contentType: "application/json"

$.ajax({
    ...
    contentType: "application/json",
    ...
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MVC .NET 上的 ajax post 未正确传递数组 的相关文章

随机推荐

  • 使用 REST 删除多条记录

    删除多个项目的 REST 方式是什么 我的用例是我有一个 Backbone Collection 其中我需要能够一次删除多个项目 选项似乎是 为每条记录发送一个 DELETE 请求 如果可能有几十个项目 这似乎是一个坏主意 发送 DELET
  • iPhone 可以支持的最大纹理尺寸是多少?

    1024 1024 或者 iPhone3gs 及以上版本只有 2048 2048 正如 iPhone 3GS 一样PowerVR SGX535 http en wikipedia org wiki IPhone ModelsGPU 最大纹理
  • 为什么在 javascript 中添加 2 + 4 时得到 24

    我正在尝试这个 function add things var first 2 var second 4 alert first second 但它给了我 24 而不是 6 我做错了什么 您正在使用 运算符连接两个字符串 尝试以下任一方法
  • JQuery 函数只能在函数内工作

    我有一个带有下拉列表的页面和 添加新 中的选项之一 为它创建的函数效果很好 town id on change function if this val ADD NEW console log Add New town id val tow
  • self 未在离子角度服务器端渲染中定义

    我正在关注https ionicframework com blog ssr with angular universal and ionic https ionicframework com blog ssr with angular u
  • 如何在控制器中使用过滤器?

    我编写了一个过滤函数 它将根据您传递的参数返回数据 我希望我的控制器具有相同的功能 是否可以在控制器中重用过滤器功能 这是我到目前为止所尝试过的 function myCtrl scope filter1 i simply used the
  • 如何使用 Maven 配置文件?

    我是 Maven 新手 在这个资源和其他资源中阅读了一些有关配置文件的信息 据我了解 如果我们想要覆盖一些默认值或做一些特定的事情 我们就使用配置文件 在我的遗留项目中 我可以看到下面的配置文件 我只看到覆盖 app mode 属性 但我没
  • 为什么我的 64 位服务以 32 位运行?

    我的 Windows 服务遇到了这个问题 在 Visual Studio 中 我已将其平台目标设置为 64 位 我已经运行 CorFlags 来验证它是否设置正确 我得到以下信息 Version v4 0 30319 CLR Header
  • ngrx EffectsModule 使 Http 服务未定义

    Using ngrx effectsv4 0 5 与 Angular v4 4 4 当我导入时EffectsModule in app module ts Http 服务变得未定义 一些代码 app module ts import Bro
  • iOS - 使用 URL 方案打开某些视图控制器

    我正在我的应用程序中使用 URL 方案 我轻松制作了一个来打开我的应用程序 只需将必要的项目添加到 info plist 即可 当前的 URL myappname 将用户带到初始视图控制器 FirstTableViewController
  • 随机添加到 Xcode 方案中的构建目标

    我正在使用 Xcode 5 和 CocoaPods 开发 iPhone 应用程序 我有一个计划和目标称为Oahu我对其进行测试 因此 看到我的两个测试目标在该方案的构建阶段构建 我并不感到惊讶 但是 有第四个构建目标无法删除 当我运行测试时
  • 在 TypeScript 的类方法中使用类型谓词

    我有一个这样定义的类 class Foo value string null constructor this value null private ensureValueExists this value is string type p
  • “String”模块对象没有属性“join”

    所以 我想在 Pygame 中创建一个用户文本输入框 我被告知要查看一个名为 inputbox 的类模块 所以我下载了 inputbox py 并导入到我的主游戏文件中 然后我在其中运行一个函数并收到错误 Traceback most re
  • CSS 选择器在 :not 选择器的最后一个匹配情况后将 css 添加到下一个位置

    这是我的 HTML 代码 div class body page check in list div class body page check in item checked a div div class body page check
  • 如何将 HTML 元素传递给 React 中的高阶函数 (HOC)?

    我经常使用HOCs https reactjs org docs higher order components html为现有的 React 组件提供附加功能 这非常简单 import Component from path to Com
  • Firebase Crashlytics 不报告崩溃

    I have 成功集成 Firebase进入我的项目 身份验证和存储效果很好 但是在简单集成 Crashlytics 并故意崩溃我的应用程序之后 而不是故意 没有崩溃报告出现在崩溃仪表板中 我确实在 Logcat 中看到 D Firebas
  • Shopify CDN 的资产 URL 是如何生成的?

    假设我上传了一张名为 logo png 的图像到 Shopify 上的商店 在 Liquid 模板中 我可以为其生成一个 URL 如下所示 logo png asset url 这会生成一个 URL 例如 http static shopi
  • 使用 sql 连接查询还是使用 pandas 合并查询,哪一种更高效?

    我想在一个表中使用多个表中的数据pandas dataframe 我有两种从服务器下载数据的想法 一种方法是使用SQL连接和检索数据 一种方法是单独下载数据帧并使用 pandas merge 合并它们 SQL Join 当我想将数据下载到p
  • 脚本运行时执行时间限制

    我的 Google Apps 脚本正在迭代用户的 Google 云端硬盘文件 并将文件复制 有时将其移动到其他文件夹 该脚本总是在几分钟后停止 并且日志中没有错误消息 编者注 时间限制随着时间的推移而变化 并且 消费者 免费 和 工作空间
  • MVC .NET 上的 ajax post 未正确传递数组

    我有一个简单的模式 它使用 select2 从服务器获取产品列表 用户可以选择多个产品 然后点击 确定 来优化搜索 我的以下设置从模式中获取数据 并使用强类型视图模型对控制器操作执行 ajax 调用 该视图模型与 JS 尝试通过 ajax