简体   繁体   English

Ajax将Null发送到.net核心MVC控制器,不确定原因

[英]Ajax sending Null to .net core MVC controller and unsure why

I am very new to Ajax and for me it's something I can't get my head around (well jQuery in general to be honest). 我对Ajax还是很陌生,对我而言,这是我无法理解的(老实说,jQuery还是不错的)。

I am trying to pass an object containing two string arrays, one of search parameters, another of search values. 我试图传递一个包含两个字符串数组的对象,一个搜索参数,另一个搜索值。

The issue, is that when i press the submit button, the Ajax call hits the MVC controller but the Model in the Controller returns Null in both arrays. 问题是,当我按下提交按钮时,Ajax调用会击中MVC控制器,但控制器中的模型在两个数组中均返回Null。

The Ajax call: Ajax调用:

    function onActivation() {
    var send = { queryParameters: queryParameters, queryValues: queryValues };
    $.ajax({
        url: '@Url.Action("Index", "SearchResults")',
        type: "POST",
        contentType: "application/json; charset=utf-8;",
        data: JSON.stringify({ advancedSearch: send }),
        success: function (response) {
            response ? alert("It worked!") : alert("It didn't work.");
        }
    });
}

The Model: 该模型:

public class AdvancedSearch
    {
        public string[] queryParameters { get; set; }
        public string[] queryValues { get; set; }
    }

The first line of the controller where things go wrong: 控制器出问题的第一行:

public async Task<IActionResult> Index(AdvancedSearch advancedSearch)

For this to work you need to submit the Ajax with just data: send . 为此,您只需要提交Ajax data: send For the model data binding to happen you can try to add another parameter in Ajax which is traditional: true as suggested in Send array via Ajax and bind to List<string> on MVC 4 为了进行模型数据绑定,您可以尝试在Ajax中添加traditional: true另一个参数traditional: true通过Ajax发送数组中所建议, 并绑定到MVC 4上的List <string>

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

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