0
我有一個包含jQuery ajax函數的剃鬚刀頁面我想將模型作爲參數發送給。我在許多地方使用$('form')。serialize()方法來設置參數。但是,在這裏我想傳遞三個特定的參數。第二個正在按預期工作,但無論我做什麼,模型總是以空值形式出現。任何人都可以指出我做錯了什麼?jQuery ajax通過模型作爲參數
//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聲明。我曾嘗試在JSON.stringify中封裝$('form')。serialize(),並且沒有做到這一點。很明顯,它必須接近它發現的方法,只是該模型爲空!
添加聲明'UpdateMailhandler'動作。 – alexmac
@Alexander該方法存在並被調用。它只是沒有按預期傳遞模型變量。我最終將我的html分成小的形式,並使用數據:$('form')。serialize()確保持有我所查找的值的控件的名稱與方法中的變量名完全相同。它以這種方式工作,模型如預期般完成。 –