我有一个简单的模式,它使用 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:
以下是 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 的大小写(这实际上使日期不再绑定)
我究竟做错了什么?