将数组发送到 MVC 中的模型时出现问题

本文关键字:模型 问题 数组 MVC | 更新日期: 2023-09-27 17:56:48

我有什么:

像这样的模型:

public class ProductModel
{
    public string ProductName { get; set; }
    public List<string> SkipUpdates { get; set; }
}

像这样的控制器方法:

public ActionResult GetProductUpdates(ProductModel productModel)
{
   return View("AddEdit");
}

现在不做任何事情只是想确保来自JS的数据正确输入。

JS:

function productModel() {
    this.productName = "";
    this.SkipUpdates = [];
}

填充模型和 AJAX:

var newProductModel = new productModel();
var options = $('#AdditionalSkipDates  option');
        var skipDates = [];
        options.each(function (i, option) {
            skipDates[i] = $(option).text();
        });
newProductModel.productName = "ABC";
newProductModel.SkipUpdates = skipDates;
$.ajax({
        type: "GET",
        url: urlToGetProductSchedule,
        data: newProductModel,
        dataType: "json"
    }).success(function () {
    }).fail(function (xhr) {
        alert("Something went wrong!!!");
        console.log(xhr.responseText);
    });

AdditonalSkip dates是一个列表框,里面有一堆日期。

发生了什么事情:

SkipUpdates 数组确实具有我可以在浏览器控制台中看到的值。在控制器中放置一个断点,它将命中该方法。"跳过更新"值为空。

未发生的情况:

如何让阵列进入控制器?

提前谢谢。

将数组发送到 MVC 中的模型时出现问题

在发送对象之前尝试将对象字符串化:

$.ajax({
        type: "GET",
        url: urlToGetProductSchedule,
        data: JSON.stringify(newProductModel),
        dataType: "json"
    }).success(function () {
    }).fail(function (xhr) {
        alert("Something went wrong!!!");
        console.log(xhr.responseText);
    });