为什么我的MVC动作不是模型绑定我的集合
Why is my MVC Action not model binding my collection?
在我的网站上,我收集了一组数字(用于排序),并将它们发送到我的MVC操作,代码如下:
$('#saveButton').click(function () {
var configId = $('#ConfigId').val();
var steps = new Array();
$('#stepList li').each(function (index) {
steps[index] = $(this).attr('stepId');
});
// Send the steps via ajax
$.ajax({
url: '" + Url.Action(MVC.GrmDeployment.EditStep.Reorder()) + @"',
type: 'POST',
dataType: 'json',
data: { configId: configId, stepIds: steps },
success: function (data) {
if (data.success) {
alert('Reorder was successful');
}
else {
alert(data.msg);
}
}
});
});
通过chrome我看到这发送以下数据通过电线:
configId:1
stepIds%5B%5D:3
stepIds%5B%5D:2
在我的控制器中,我有以下方法来接收值
public virtual ActionResult Reorder(int configId, ICollection<int> stepIds) { }
问题是stepIds
集合为空。有人知道原因吗?
我记得,jQuery改变了它在ajax
方法中编码数组的方式,所以为了继续与MVC兼容,您必须将traditional
选项设置为true
:
$.ajax({
url: '@Url.Action(MVC.GrmDeployment.EditStep.Reorder())',
type: 'POST',
dataType: 'json',
traditional: true, // This is required for certain complex objects to work with MVC AJAX.
data: { configId: configId, stepIds: steps },
success: function (data) {
if (data.success) {
alert('Reorder was successful');
}
else {
alert(data.msg);
}
}
});
使用JSON。Stringify:
var viewModel = new Object();
viewModel.configId = $('#ConfigId').val();
viewModel.steps = new Array();
data: { JSON.Stringify(viewModel) },
我已经将动作绑定到数组。我个人有一个带有字符串[]参数的操作。
试
public virtual ActionResult Reorder(int configId, int[] stepIds) { }
相关文章:
- Angular没有根据模型更新我的选择元素
- 在这种情况下,我应该如何在余烬中放置我的模型有很多关系
- 当输入键由jquery触发时,我的模型是未定义的
- 如果ng-if的计算结果为false,AngularJS会使我的模型数据无效
- MVC4剃刀在客户端javascript中访问我的模型中的JsonResult数据
- 无法在 javascript 中使用我的模型数据
- 为什么我的模型中的默认属性返回未定义
- 在 FTL 中向我的模型发送一个 JavaScript var
- Backbone fetch返回我的模型的所有实例
- 用Backbone.js设置我的模型
- 如果我的模型是MVC3中的QueryTable,如何访问Javascript中的项目数据
- 如何使用Parse cloud js保存我的模型
- 我的模型应该有多细粒度
- 当试图访问我的模型上的方法时,得到Javascript TypeError
- 我的dom不反映我的模型更新值,更新是由一个函数,这是按钮单击CAL完成的
- Backbone.js,require.js:我如何将我的模型、视图和集合转换为require.js模块
- 为什么我的模型值为空,如果我不包装脚本代码中的单击事件
- 我如何验证我的模型,当我有一个数组属性
- 为什么我不能从javascript访问我的模型对象
- ng-repeat不能用Object.create声明我的模型