gpt4 book ai didi

javascript - MVC .NET 上的 ajax 帖子未正确传递数组

转载 作者:搜寻专家 更新时间:2023-11-01 04:36:51 24 4
gpt4 key购买 nike

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

我的以下设置从模式中获取数据,并使用与 JS 尝试通过 ajax 调用发送的内容相匹配的强类型 View 模型对 Controller 操作执行 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>');
}
});

就在 ajax 调用转到 Controller 之前,我检查了 exploreFilters 的内容和结构: exploreFilters content and structure

这是表单数据在 POST 请求中的样子:

Form data

另一方面,我得到了一个 Controller ,它采用一个结构类似于 exploreFilters 的强类型参数:

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

还有我的强类型 View 模型:

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

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

inspecting data bound to strongly typed view

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

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

我做错了什么?

最佳答案

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

$.ajax({
...
contentType: "application/json",
...
});

关于javascript - MVC .NET 上的 ajax 帖子未正确传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25212591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com