MVC .NET上的ajax帖子没有正确传递数组 [英] ajax post on MVC .NET does not pass array correctly

查看:103
本文介绍了MVC .NET上的ajax帖子没有正确传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple modal that uses select2 to get a list of Products from the server. User can multiple choose products and hit Ok to refine a search.

我的以下设置从模态中获取数据并针对具有强类型的Controller操作执行ajax调用查看模型,它匹配JS试图通过ajax调用发送的内容。

My following setup grabs the data from the modal and does an ajax call against a Controller action with a strongly typed view model that matches what the JS is trying to send via the ajax call.

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调用进入控制器之前,我检查了exploreFilters的内容和结构:

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

以下是表单数据在POST请求中查找:

Here is how the form data looks on the POST request:

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

On the other side I got a controller which takes a strongly-typed parameter with a structure similar to what exploreFilters has:

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已被成功绑定但不是我的产品清单。

Once the controller action gets hit I can see that DateStart and DateEnd have been successfully bound but not my list of products.

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

I cannot change the datatype on the json request, it has to be html because my controller action is going to be returning html.

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

I've tried changing the capitalization on Id and Text, JSON.stringify (which actually makes the dates not bind anymore)

我做错了什么?

推荐答案

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

Try to add contentType: "application/json" in your Ajax request

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

这篇关于MVC .NET上的ajax帖子没有正确传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆