gpt4 book ai didi

jquery - 将 List 作为参数传递给 MVC Controller

转载 作者:行者123 更新时间:2023-12-01 05:21:48 24 4
gpt4 key购买 nike

我尝试在代码中到处做一件事情,但由于未知的原因我不能在这里做。

我尝试将对象列表传递到 Controller ,但无法将数组映射到列表中。

查看:

var interventions = [
{
Id: 1,
Title: 'Intervention 1',
ToDoBefore: new Date(),
PlannedDate: new Date()
},
{
Id: 2,
Title: 'Intervention 2',
ToDoBefore: new Date(),
PlannedDate: new Date()
},
]

$.ajax({
url: '/Home/AffectToSubcontractors',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
data: JSON.stringify(interventions),
success: function (result) {
debugger;
}
});

Controller :

    public PartialViewResult AffectToSubcontractors(List<SelectedInterventionsViewModel> interventions)
{
// Do something
}

型号:

public class SelectedInterventionsViewModel
{
public int Id { get; set; }

public string Title { get; set; }

public DateTime ToDoBefore { get; set; }

public DateTime PlannedDate { get; set; }
}

我想念什么?

最佳答案

我找不到您的代码中缺少的任何内容,但我已经尝试了您提到的相同代码并找到了该集合。 C# 部分不能有错误, View 中可能存在一些问题。我在index.chtml上尝试了如下操作,请在干净的 View 中尝试您的代码。

 @ViewBag.Title  Home
<div>
<div>
<script >
var interventions = [
{
Id: 1,
Title: 'Intervention 1',
ToDoBefore: new Date(),
PlannedDate: new Date()
},
{
Id: 2,
Title: 'Intervention 2',
ToDoBefore: new Date(),
PlannedDate: new Date()
},
]

$.ajax({
url: '/Home/AffectToSubcontractors',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
data: JSON.stringify(interventions),
success: function (result) {
debugger;
}
});
</script>
</div>
</div>

关于jquery - 将 List 作为参数传递给 MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42840458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com