【发布时间】:2014-02-16 01:48:50
【问题描述】:
我有一个包含 jQuery ajax 函数的剃须刀页面,我想将模型作为参数发送到。我在许多地方使用 $('form').serialize() ,这是设置参数的唯一方法。但是,这里我想传递三个具体的参数。后两个按预期工作,但无论我做什么,模型总是以空值出现。谁能指出我做错了什么?
//modal button click events to update mailhandlers for selected accounts
$('#updateMailhandler').click(function () {
//add the selected ids to the hidden control to pass values to controller
var selectedIds = "";
$('[name$=".selected"]').each(function () {
if (this.checked) {
selectedIds = selectedIds + ',' + $.trim($(this).parent().parent().find('.accountid').text());
}
});
selectedIds = selectedIds.substring(1);
//retrieve selected mailhandlerid
var mhid = $('#mailhandlerId').val();
//execute the controller method
$.ajax({
url: '@Url.Action("UpdateMailhandler", "SendingReport")',
dataType: 'json',
data: { model: $('form').serialize(), selectedAccountString: selectedIds, mailhandlerId: mhid },
type: 'POST'
})
})
我已尝试删除 dataType 声明。我尝试将 $('form').serialize() 包装在 JSON.stringify 中,但什么也没做。显然,这必须是接近的,因为它找到了方法,只是那个模型是空的!
【问题讨论】:
-
添加声明
UpdateMailhandler操作。 -
@Alexander 该方法存在并被调用。它只是没有按预期传递模型变量。我最终将我的 html 拆分为小表单并仅使用 data: $('form').serialize() 确保保存我要查找的值的控件与方法中的变量名称具有完全相同的名称。它以这种方式工作,并且模型按预期通过。
标签: javascript jquery ajax json razor