C# 使用jQuery将多个对象发送到MVC 3控制器

C# 使用jQuery将多个对象发送到MVC 3控制器,c#,jquery,.net,asp.net-mvc-3,C#,Jquery,.net,Asp.net Mvc 3,我有一个复选框列表和下拉列表。我想将dropdownlist和复选框中的选定值传递给我的MVC3控制器,并使用模型绑定 复选框的ViewModel: public class StateViewModel { public int Id { get; set; } public string Name { get; set; } public bool Checked { get; set; } public IEnumerable<TransitionVi

我有一个复选框列表和下拉列表。我想将dropdownlist和复选框中的选定值传递给我的MVC3控制器,并使用模型绑定

复选框的ViewModel:

public class StateViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }
    public IEnumerable<TransitionViewModel> Transitions { get; set; }
}
我的看法是:

@using (Html.BeginForm("GetSchedules", "Home", FormMethod.Post, new { id = "checkboxFrm" }))
{
    @Html.EditorForModel()
    <input id="ShowReportScheduleBtn" type="button" value="Show schedule" onclick="ShowReportSchedules()" />
}
最后,我的控制器:

function ShowReportSchedules() {
    var selectedGenerationId = $('#SelectedGenerationId').val();
    var statesData = JSON.stringify($('#checkboxFrm'));
    var statesData2 = $('#checkboxFrm').serialize(); //I also tried this

        $.ajax({
            type: 'POST',
            url: '/Home/GetSchedules',
            data: { "generationId": selectedGenerationId, "states": statesData },
            dataType: "json",
            contentType: 'application/json; charset=utf-8'
        });
};
    [HttpPost]
    public ActionResult GetSchedules(int generationId, IEnumerable<StateViewModel> states)
    {
        return View("Index");
[HttpPost]
public ActionResult GetSchedules(int generationId,IEnumerable状态)
{
返回视图(“索引”);
我无法将值传递给我的控制器。我在表单中使用
type=“submit”
而没有jQuery的情况下只传递了
statesData
对象。当我尝试使用jQuery和
type=“button”
只传递
statesData时,我在FireBug中遇到了“无效的JSON原语”错误

当我试图传递integer+
statesData
对象时,IE 9崩溃,Firefox挂起

我尝试过各种解决方案,但都没有成功。

像这样尝试:

function ShowReportSchedules() {
    var selectedGenerationId = $('#SelectedGenerationId').val();
    $.ajax({
        type: 'POST',
        url: '@Url.Action("getschedules", "home")?generationId=' + selectedGenerationId,
        data: { states: $('#checkboxFrm').serialize() },
        success: function(result) {
            // Do something with the results
        }
    });
};

好的,现在我没有得到任何异常,generationId被传递给控制器,但状态不是。这是我在帖子中得到的:状态=%255B0%255D.Id%3D1%26%255B0%255D.Name%3DObrada%26%255B0%255D.Checked%3Dfalse%26%255B1%255D.Id%3D2%26%255B1%255D.Name%3DVerifikacija%26%255B1%255D.Checked%3Dtrue%26等。好的,我想出来了,如果你删除它就行了“data”中的花括号和参数名。如果它看起来是这样的:data:$(“#checkboxFrm”).serialize(),那么它就可以工作了(它被正确序列化了)。我会将这个答案标记为正确的,但是有没有办法通过jquery发送多个对象?
function ShowReportSchedules() {
    var selectedGenerationId = $('#SelectedGenerationId').val();
    $.ajax({
        type: 'POST',
        url: '@Url.Action("getschedules", "home")?generationId=' + selectedGenerationId,
        data: { states: $('#checkboxFrm').serialize() },
        success: function(result) {
            // Do something with the results
        }
    });
};
var a = $("#a").serialize();
var b = $("#b").serialize();
var c = $("#c").serialize();


$.ajax({
          url: '@Url.Content("~/Controller/Method1")',
          type: 'POST',
          data: a+b+c,
          success: function (success) {
            // do something
          }
        });

// in Controller
[HttpPost]
public ActionResult Method1(abc a, bcd b, xyz c)
{
}

// where abc, bcd xyz are class