从视图无法在MVC5控制器中获取复选框值为true或false

时间:2019-03-01 11:24:53

标签: ajax entity-framework asp.net-mvc-5

public class textmodel
{
    public int id { get; set; }

    [Required]
    public string text1 { get; set; }

    [Required]
    public string text2 { get; set; }

    [Required]
    public bool IsActive { get; set; }

    public string Gender { get; set; }
}

以上是我的模型课:

@model datatablebindingnew.entity.TableTest

<h2>Save</h2>

@using (Html.BeginForm("save", "home", FormMethod.Post, new { id = "popupForm" 

}))
{
    if (Model != null && Model.id > 0)
    {
        @Html.HiddenFor(a => a.id)
    }
<div class="form-group">
    <label>Text1</label>
    @Html.TextBoxFor(a => a.text1, new { @class = "form-control" })
    @Html.ValidationMessageFor(a => a.id)
</div>
<div class="form-group">
    <label>Text2</label>
    @Html.TextBoxFor(a => a.text2, new { @class = "form-control" })
    @Html.ValidationMessageFor(a => a.text2)
</div>
<div class="form-group">
    <label>IsActive</label>
    @Html.CheckBoxFor(a => a.IsActive.Value, new { @class = "form-control", @checked = "checked" })

</div>
<div class="form-group">
    <label>Gender</label>
    @Html.DropDownListFor(a=>a.Gender, new List<SelectListItem>
     {
         new SelectListItem{ Text="Male", Value="Male"},
         new SelectListItem{ Text="Female", Value="Female"}
     }, "--- Select ---", new { @class = "form-control" })



</div>

<div>
    <input type="submit" value="Save" />
</div>
}

这是我的部分视图,下面是控制器:

 [HttpPost]
        public ActionResult Save(TableTest emp)
        {
            bool status = false;
            //bool IsActive = Convert.ToBoolean(Request.Form["IsActive"]);
            if (ModelState.IsValid)
            {


                    if (emp.id > 0)
                    {
                        //Edit 
                        var v = db.TableTests.SingleOrDefault(a => a.id == emp.id && a.IsActive == false);
                        if (v != null)
                        {
                            v.text1 = emp.text1;
                            v.text2 = emp.text2;
                        v.IsActive = emp.IsActive;
                        v.Gender = emp.Gender;

                        }
                    }
                    else
                    {
                        //Save
                        db.TableTests.Add(emp);
                    }
                    db.SaveChanges();
                    status = true;

            }
            return new JsonResult { Data = new { status = status } };
        }

这是我关于ajax帖子的主要观点:

$('.popupWindow').on('submit', '#popupForm', function (e) {
                debugger
                var checkboxData = $(':checked').val();
                var url = $('#popupForm')[0].action;
                $.ajax({
                    type: "POST",
                    url: url,
                    data: $('#popupForm').serialize(),
                    success: function (data) {

                        if (data.status) {
                            $dialog.dialog('close');

                        }
                    }
                })

                e.preventDefault();
            })
            $dialog.dialog('open');
        }
    })

上面的代码是我的代码,每当我提交表单时,我的控制器和数据库中的复选框值都为null,复选框字段IsActive中的值也为null

0 个答案:

没有答案