checkboxfor bind to model ajax postback

时间:2014-12-17 10:42:25

标签: c# jquery asp.net-mvc

我试图通过我的控制器中的ajax回发来获取复选框的复选框,但即使在单击按钮控件之前在视图中选中了复选框,它也会给我错误。

我的代码如下:

[HttpPost]
public ActionResult CivilSurveys(CivilSurveyViewModel modelData)
{
  try
  {
    var s = Request["SurveyAccepted"].Contains("true");
    if (modelData.SurveyAccepted)
      {
        modelData.SurveyAcceptedBy = int.Parse(Session["SessionUserId"].ToString());
        modelData.SiteDiagram = ViewBag.Image;
        modelData = (BAL.Surveys.GetCivilSurvey(modelData));
      }
      return View(modelData);
    }
    catch (Exception ex)
    {
      Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
      return View();
    }
  }

查看

@using (Html.BeginForm("", "", FormMethod.Post, new { id = "frmCivilSurveys" }))
{
  <div class="ControlSet">
    @Html.LabelFor(model => model.SurveyAcceptedBy):
    @Html.CheckBoxFor(model => model.SurveyAccepted, new { style = "margin-left:198px;"     })
  </div>
  <div style="clear: both; margin: 10px; padding-top: 10px; overflow: hidden;">
    <input type="submit" class="k-button" value="Create" style="float: right;" id="btnCreateCivilSurvey" />
    <input type="button" class="k-button" value="Update" id="btnEditCivilSurvey" style="float: right;" />
  </div>

<script>
  $("#btnCreateCivilSurvey").click(function() {
    if ($("#frmCivilSurveys").valid()) {
      $.ajax({
        type: "POST",
        url: "@Url.Action("CivilSurveys", "Civils")",
        data: JSON.stringify($('#frmCivilSurveys').serializeObject()),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: true,
        cache: false,
        success: function (msg) {
          if (msg.status == "1") {       }
            alert(msg.message);
          }
        });
      } else {
        $($("#frmCivilSurveys").validate().errorList[0].element).focus();
      }
      return false;
  });
</script>

这是因为使用checkboxfor时自动处理的隐藏字段。

我已经通过使用类型复选框的输入解决了这个问题,并在foreach循环中为它们设置了id&#39; s,如下所示:

@foreach (var item in Model.SurveyTypeList)
{
  @Html.Label(item.Text)
  <input name="SurveyTypeList[@i].Text" type="hidden" value="@item.Text" />
  <input name="SurveyTypeList[@i].Value" type="hidden" value="@item.Value" />
 <input data-val="true" id="SurveyTypeList@(i)__IsSelected" name="SurveyTypeList[@i].IsSelected"          style="margin-right: 200px; float: right" type="checkbox" value="true" />
  <br /><br />
  i++;
  }

但这不是一个完美的解决方案,我需要一些更完美的解决方案

2 个答案:

答案 0 :(得分:0)

这是因为使用checkboxfor时自动处理的隐藏字段。

我已经通过使用类型复选框的输入解决了这个问题,并在foreach循环中为它们设置了id&#39; s,如下所示:

 @foreach (var item in Model.SurveyTypeList)
 {
  @Html.Label(item.Text)
  <input name="SurveyTypeList[@i].Text" type="hidden" value="@item.Text" />
   <input name="SurveyTypeList[@i].Value" type="hidden" value="@item.Value" />
  <input data-val="true" id="SurveyTypeList@(i)__IsSelected" name="SurveyTypeList[@i].IsSelected"      style="margin-right: 200px; float: right" type="checkbox" value="true" />
 <br /><br />
  i++;
  }

但这不是一个完美的解决方案,我需要一些更完美的解决方案

答案 1 :(得分:0)

将ajax调用更改为

$.ajax({
    type: "POST",
    url: "@Url.Action("CivilSurveys", "Civils")",
    data: $('#frmCivilSurveys')),
    // dataType: "json", - Note you appear to be returning a view so this should be html
    cache: false,
    success: function (msg) {
      ....

关键点

  1. 使用.serialize(),而不是serializObject()
  2. 不要使用JSON.stringify
  3. 删除contentType: "application/json; charset=utf-8"