简体   繁体   English

将数组从JavaScript传递到Asp.net Core

[英]Passing Array from JavaScript to Asp.net Core

Here is code for javascript which calls asp.net CustomHeatMapDate 这是调用asp.net CustomHeatMapDate的javascript代码

$('.Date').change(function () {
    var data = [];
    console.log();
    var dateList = [{"Date":"03/23/2016"}, {"Date":"03/24/2016"}];
    $.ajax({
        async: true,
        type: "GET",
       url: "/Home/CustomHeatMapDate",
       data: { Date: dateList },
        dataType: "json",
        success: function (data) {
            console.log(data);
            for (var i = 0, len = data.length; i < len; i++) {
                pushdata(data[i]);
            }
        }
    })

Here is the Asp.net Controller 这是Asp.net控制器

public IActionResult CustomHeatMapDate(Array[] Date)
{           
    return View();
}

But Date Array is null 但是日期数组为空

There are a few issues, I'll go through them and explain why and how to fix them. 这里有一些问题,我将逐一介绍并解释原因以及如何解决。

The first one is the data you are sending to the controller, you've structured it so the outcome will be {Date:[{"Date":"03/23/2016"}, {"Date":"03/24/2016"}]} when it should look like this {Date:["03/23/2016", "03/24/2016"]} then you will be sending a flat array up instead of an array of objects. 第一个是要发送到控制器的数据,已经对其进行了结构化,因此结果将为{Date:[{"Date":"03/23/2016"}, {"Date":"03/24/2016"}]}看起来应该 {Date:["03/23/2016", "03/24/2016"]}那么您将发送一个平面数组,而不是对象数组。

The second issue is your datatype in your controller, you've got it set to Array[] This is expecting and array of arrays. 第二个问题是控制器中的数据类型,您已将其设置为Array[]这是数组的期望值。 What you should have is DateTime[] , this will now expect an array of date(times). 您应该拥有的是DateTime[] ,现在将需要一个日期(时间)数组。 This will mean you'll have to change your dates being sent up to 2016-03-23 so it can be deserialized properly. 这意味着您必须更改发送日期,直到2016-03-23这样它才能正确反序列化。

The last is the fact that you're doing this as a GET which doesn't really handle passing structured data in the parameters as it puts it all in the query string, which as your array of dates gets longer you may run the risk of it becoming too long. 最后一个事实是,您以GET ,实际上并不能真正处理参数中传递的结构化数据,因为它会将所有数据都放入查询字符串中,随着日期数组的变长,您可能会面临以下风险:它变得太长了。 I'd recommend using a POST instead, although if you really do want to do a GET then as Alexandru-Ionut Mihai said add traditional:true to the ajax properties 我建议使用POST代替,尽管如果您确实要执行GET那么正如Alexandru-Ionut Mihai所说的,在ajax属性中添加traditional:true

TL;DR The data structure on the client and server side are wrong TL; DR客户端和服务器端的数据结构错误

Putting it all together your code should look something like this 将所有内容放在一起,代码应如下所示

JS JS

$('.Date').change(function () {
    console.log();
    var dateList = ["2016-03-23", "2016-03-24"];
    $.ajax({
        type: "POST",
        url: "/Home/CustomHeatMapDate",
        data: {
            Date: dateList
        },
        dataType: "json",
        success: function (data) {
            console.log(data);
            for (var i = 0, len = data.length; i < len; i++) {
                pushdata(data[i]);
            }
        }
    })
});

C# C#

public ActionResult CustomHeatMapDate(DateTime[] Date)
{
    return Ok();
}

PS there is no need to specify async:true as by default JQuery Ajax will set it to true. PS无需指定async:true ,因为默认情况下,JQuery Ajax会将其设置为true。

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

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