简体   繁体   English

模型绑定到AJAX突然无法正常工作

[英]Model Binding to AJAX suddenly not working

I have an AJAX POST request which is supposed to send a user's search filters off to a controller action and return appropriate results. 我有一个AJAX POST请求,它应该将用户的搜索过滤器发送到控制器操作并返回适当的结果。 This was working perfectly, then it just stopped working. 这完美地工作,然后它就停止了工作。 I haven't changed a thing, it was working fine, the model was binding exactly as intended, then it just broke and now it will only bind to the non-array items. 我没有改变一件事,它工作正常,模型完全按照预期绑定,然后它刚刚破坏,现在它只会绑定到非数组项。

Here is my full Jquery AJAX code: 这是我完整的Jquery AJAX代码:

var parent = $('.recipe-search > .search-form > .filter-panel > .area');
    var searchobj = {
        SearchTerm: $(parent).find('input.search-terms').val(),
        TimeRange:
            [
                $(parent).find('div.time-range').slider("values", 0),
                $(parent).find('div.time-range').slider("values", 1)
            ],
        DifficultyRange: [
            $(parent).find('div.difficulty-range').slider("values", 0),
            $(parent).find('div.difficulty-range').slider("values", 1)
        ],
        Vegetarian: $(parent).find('input.vegetarian').is(':checked'),
        Vegan: $(parent).find('input.vegan').is(':checked'),
        DietTags: $(parent).find('input.diet-tags').val().split(' ')
    };

    $.ajax({
        type: "POST",
        url: $('.recipe-search > .search-form').attr('action'),
        data: searchobj,
        error: function () {
            alert("There was an error getting your search results, please try again");
        },
        success: function (result) {
            popresults(result);
        }
    });

Here is the C# model: 这是C#模型:

For anyone wondering the sliders are JQuery UI sliders. 对于任何想知道滑块是JQuery UI滑块的人。

In the Chrome networking tab, I can see the request gets sent off as intended 在Chrome网络标签中,我可以看到请求按预期发送

However looking at the debug watch window when a breakpoint is hit, it is clear that they are not being bound to the model correctly 但是,当遇到断点时查看调试监视窗口,很明显它们没有正确绑定到模型

I'll repeat myself, and I know some of you will call me out on this saying that I did change something, I must have, but I did not change a thing between it working exactly as intended, and it not working. 我会重复一遍,而且我知道你们中的一些人会在这个问题上叫我说我确实改变了一些东西,我必须这样做,但我并没有改变它与预期完全一致的工作,而且它没有用。

Something that does strike me as odd is that the arrays sent in the request (look at the network tab photo) appear to be sending the two values as individual properties instead of values in an array. 确实令我感到奇怪的是,请求中发送的数组(查看网络选项卡照片)似乎是将两个值作为单独的属性而不是数组中的值发送。

Well it seems that adding two lines to the AJAX code fixed this: 好吧,似乎在AJAX代码中添加两行修复了这个问题:

datatype: "json", traditional: true

Now it works completely as intended. 现在它完全按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM