【发布时间】:2011-07-14 16:28:52
【问题描述】:
我有一个复选框列表和下拉列表。我想将下拉列表中的选定值和选中的复选框传递给我的 MVC3 控制器并使用模型绑定。
复选框的视图模型:
public class StateViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
public IEnumerable<TransitionViewModel> Transitions { get; set; }
}
StateViewModel /Views/Home/EditorTemplates/StateViewModel.cshtml 的编辑器模板:
@model Mcs.Sibs.UI.Web.Models.StateViewModel
@Html.HiddenFor(x => x.Id)
@Html.HiddenFor(x => x.Name)
<div>
@Html.CheckBoxFor(x => x.Checked)
@Html.LabelFor(x => x.Checked, Model.Name)
</div>
我的看法:
@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");
我无法将值传递给我的控制器。我已经设法在没有 jQuery 的情况下仅传递 statesData 对象,并在我的表单中使用 type="submit"。当我尝试仅使用 jQuery 和 type="button" 传递 statesData 时,FireBug 中出现“无效 JSON 原语”错误。
当我尝试传递整数 + statesData 对象时,IE 9 崩溃并且 Firefox 挂起。
我尝试了各种解决方案,但都没有成功。
【问题讨论】:
标签: c# jquery .net asp.net-mvc-3