ASP.NET Ajax Serialize Form并将其他参数传递给Controller

时间:2016-11-29 19:11:46

标签: javascript c# jquery asp.net ajax

我想发送的不只是序列化的表单数据到控制器。例如,我想包含其他参数。类似的东西:

    data: {record:formData, searchLetter:letter, page:pageNum}

但是,每当我尝试这个时,模型都没有正确绑定,最终会为空。

使用Javascript:

$(document).ready(function () {
$('.save-attendance-record-button').click(function() {

        $('#form-edit-attendance-record').on('submit', function(e) {
        e.preventDefault();
        return false;

        var formData = $(this).serialize();
        $.ajax({
            dataType: 'html',
            method: 'post',
            url: resolveUrl('~/Home/EditAttendanceRecord'),
            data: { record:formData, searchLetter:"a", searchTerm:"", page:1 }
        })
        .done(function (data) {
            e.preventDefault();
            $('#attendance-records-partial').html(data);
        });
        e.preventDefault();
    });

    $('#form-edit-attendance-record').submit();  
});
});

控制器:

[HttpPost]
public ActionResult EditAttendanceRecord(AttendanceRecord record, string searchLetter, int? page)
{
     //...
}

修改1

这是formData在唯一参数时的样子:

ID=2&InviteeFirstName=Test%20K.&InviteeLastName=Test&InviteeCheckedIn=true&InviteeCheckedIn=false&GuestName=test&GuestCheckedIn=true&GuestCheckedIn=false&RSVP=

如果我有多个参数(这就是我想要的),请求标题如下所示:

record=ID%3D2%26InviteeFirstName%3DTest%2520K.%26InviteeLastName%3DTest%26InviteeCheckedIn%3Dfalse%26GuestName%3DWOW%26GuestCheckedIn%3Dtrue%26GuestCheckedIn%3Dfalse%26RSVP%3D&searchLetter=a&searchTerm=&page=1 

或解析:

record:ID%3D2%26InviteeFirstName%3DTest%2520K.%26InviteeLastName%3DTest%26InviteeCheckedIn%3Dfalse%26GuestName%3DWOW%26GuestCheckedIn%3Dtrue%26GuestCheckedIn%3Dfalse%26RSVP%3D 
searchLetter:a 
searchTerm: 
page:1

0 个答案:

没有答案