Javascript 在MVC中将数组发送到模型时出现问题
我所拥有的: 像这样的模型:Javascript 在MVC中将数组发送到模型时出现问题,javascript,c#,arrays,asp.net-mvc,Javascript,C#,Arrays,Asp.net Mvc,我所拥有的: 像这样的模型: public class ProductModel { public string ProductName { get; set; } public List<string> SkipUpdates { get; set; } } public ActionResult GetProductUpdates(ProductModel productModel) { return View("AddEdit"); } 现在不做任何事
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数组确实有我可以在浏览器控制台中看到的值。
在控制器中放置一个断点,它将命中该方法。
SkipUpdates值为空
没有发生的事情:
如何使阵列进入控制器
提前感谢。在发送对象之前,请尝试将其字符串化:
$.ajax({
type: "GET",
url: urlToGetProductSchedule,
data: JSON.stringify(newProductModel),
dataType: "json"
}).success(function () {
}).fail(function (xhr) {
alert("Something went wrong!!!");
console.log(xhr.responseText);
});
您没有向集合值添加索引器。您可以查看字符串化数据(JSON.stringify()
)和/或使用
traditional:true,`选项。但是,你为什么要发回所有选项(而不仅仅是选定的值?)斯蒂芬,不管是所有选项还是选定的选项都被发回。是的,数组将包含所有选项,如果id有效,您将无法知道选择了哪些选项。