复选框值始终为false ASP MVC 5

时间:2017-01-24 07:14:13

标签: javascript jquery asp.net asp.net-mvc

我有一个问题。

无论我做什么,我的复选框值都是假的。

我使用这个模型

public class ActEntry : BaseEntity
{
    [Required]
    public int DepartmentId { get; set; }

    [Required]
    [Display(Name = "Тип")]
    public EnergyType EnergyType { get; set; }

    [Required]
    [Display(Name = "Имя объекта установки")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "Позиция")]
    public string Postition { get; set; }

    [Required]
    [Display(Name = "Проектная")]
    public bool HasDoc1 { get; set; }

    [Required]
    [Display(Name = "Исполнительная")]
    public bool HasDoc2 { get; set; }

    [Required]
    [Display(Name = "Статус")]
    public State State { get; set; }

    [Display(Name = "Дата сдачи")]
    public DateTime? DateOfCommit { get; set; }

    [Required]
    [Display(Name = "Акт допуска")]
    public string Act { get; set; }

    [Required]
    [Display(Name = "Год акта")]
    public int Year { get; set; }
}

我的表格:

<div class="modal fade" id="editRow">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h2 class="modal-title" id="modalTitle"></h2>
      </div>
      <div class="modal-body">
        <form class="form-horizontal" method="post" id="detailsForm">
          <input type="hidden" id="Id" name="Id">
          <input type="hidden" id="DepartmentId" name="DepartmentId" value="@Model.DepartmentId">
          <input type="hidden" id="EnergyType" name="EnergyType" value="@Model.EnergyType">
          <div class="form-group">
            <label for="Name" class="control-label col-xs-3">Имя объекта установки</label>
            <div class="col-xs-9">
              <input class="form-control" id="Name" name="Name">
            </div>
          </div>
          <div class="form-group">
            <label for="Postition" class="control-label col-xs-3">Позиция</label>
            <div class="col-xs-9">
              <input type="text" class="form-control" id="Postition" name="Postition">
            </div>
          </div>
          <div class="form-group">
            <label for="Postition" class="control-label col-xs-3">Наличие документации</label>
            <div class="col-xs-9">
              <label class="checkbox-inline">
                <input type="checkbox" name="HasDoc1" value="true">Проектная</label>
              <label class="checkbox-inline">
                <input type="checkbox" name="HasDoc2" value="true">Исполнительная</label>
            </div>
          </div>

          <div class="form-group">
            <label for="Act" class="control-label col-xs-3">Акт допуска</label>
            <div class="col-xs-9">
              <input type="text" class="form-control" id="Act" name="Act">
            </div>
          </div>
          <div class="form-group">
            <label for="Year" class="control-label col-xs-3">Год акта</label>
            <div class="col-xs-9">
              <input type="text" class="form-control" id="Year" name="Year">
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-xs-3"></label>
            <div class="col-xs-9">
              <button class="btn btn-primary" type="button" onclick="save()">Сохранить</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

以下是这些复选框:

<label class="checkbox-inline">
  <input type="checkbox" name="HasDoc1" value="true">
  <input type="hidden" name="HasDoc1" value="false">Проектная</label>
<label class="checkbox-inline">
  <input type="checkbox" name="HasDoc2" value="true">
  <input type="hidden" name="HasDoc2" value="false">Исполнительная</label>

我的ajax脚本:

function save() {
  if (!$('#detailsForm').valid()) return false;

  $.ajax({
    type: "POST",
    url: "/Ajax/CreateOrUpdate/",
    data: $('#detailsForm').serialize(),
    success: function() {
      $('#editRow').modal('hide');
      updateTable();
      successNoty('Успешно сохранено')
    }
  });
};

当我看到序列化值时,我注意到HasDoc2&amp; HasDoc1是假的。如果我在Chrome中检查它们并使用$('#HasDoc1').val()&amp; $('#HasDoc2').val()我总是假的。

当我将他们的名字改为 hasDoc1 &amp; hasDoc2 ,当我检查它们时,突然间我得到了。我做错了什么?

ASP MVC中的复选框属性命名是否有特殊规则? 我尝试使用助手,结果是一样的。

3 个答案:

答案 0 :(得分:0)

您为两个输入(复选框和隐藏)使用相同的名称,并且使用JQuery,您可以使用imshow获取复选框值。

prop("checked")方法返回val()属性的值,请参阅以下示例:

&#13;
&#13;
value
&#13;
&#13;
&#13;

我希望它可以帮助你,再见。

答案 1 :(得分:0)

Do not use same ID in same document, because it is against the specification.IDs should be unique.Here's a basic example: https://jsfiddle.net/dhirenpateldev/dke0s8nL/6/

答案 2 :(得分:0)

使用此:

<input type="checkbox" id="HasDoc1" name="HasDoc1"
       value="@(Model.HasDoc1 ? "true" : "false")"
       onchange="$(this).val(this.checked)"
       @(Model.HasDoc1 ? "checked=checked" : "") />

或者以更好的方式为布尔值编写EditorTemplate:

@model bool

<input type="checkbox" id="@Html.IdForModel()" name="@Html.NameForModel()"
       value="@(Model ? "true" : "false")"
       onchange="$('#@(Html.IdForModel())').val(this.checked)"
       @(Model ? "checked=checked" : "") />

并使用它:

@Html.EditorFor(p => p.HasDoc1)