1

I have been trying to pass data to the controller from the form but it gives me null values. I have my model/controller and view as below. I even tried to use Form Collection but why is this not binding the data properly?

Role.cs

 public partial class Role
    {
        public int Role_Id { get; set; }
        public int EmpID { get; set; }
        public Nullable<bool> Door_Unlock { get; set; }
        public Nullable<bool> Accounts { get; set; }
        public Nullable<bool> Bounds_Email { get; set; }
        public Nullable<bool> Salary_Privilege { get; set; }
        public Nullable<bool> Card_Acceptance { get; set; }
        public Nullable<bool> IsAdmin { get; set; }

        public virtual Employee Employee { get; set; }
    }

My controller is;

RoleController:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "EmpID,Door_Unlock,Accounts,Bounds_Email,Salary_Privilege,Card_Acceptance,IsAdmin")] Role roles)
        {
 if (ModelState.IsValid)
                {

                    //db.Roles.Add(roles);
                   // db.SaveChanges();
                    return RedirectToAction("Index");
                }

Create.cshtml

@model xxxxx.Models.Role

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>
@*@using (Html.BeginForm())*@
<form method="POST">


    @Html.AntiForgeryToken()
<div class="form-horizontal">
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.FirstName, "Select Employee", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("EnrollNumber", null, "-Select Employee-", htmlAttributes: new { id = "ddEnrollNumber", @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Employee.FirstName, "", new { @class = "text-danger" })

        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Accounts, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor( model => model.Accounts.Value, new { name = "accountsCheck", @class = "accountsCheck", @checked = "checked" })
            @Html.ValidationMessageFor(model => model.Accounts, "", new { @class = "text-danger" })

        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Bounds_Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Bounds_Email.Value, new { @class = "boundsCheck", @checked = "checked" })
            @Html.ValidationMessageFor(model => model.Bounds_Email, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Card_Acceptance, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Card_Acceptance.Value, new { @class = "cardCheck", @checked = "checked" })
            @Html.ValidationMessageFor(model => model.Card_Acceptance, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Door_Unlock, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Door_Unlock.Value, new { @class = "doorUnlockCheck", @checked = "checked" })
            @Html.ValidationMessageFor(model => model.Door_Unlock, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Salary_Privilege, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Salary_Privilege.Value, new { @class = "salaryCheck", @checked = "checked" })
            @Html.ValidationMessageFor(model => model.Salary_Privilege, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.IsAdmin, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.IsAdmin.Value, new { @class = "isAdminCheck", @checked = "checked" })
            @Html.ValidationMessageFor(model => model.IsAdmin, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Grant Permissions" class="btn green" />
        </div>
    </div>
    </div>
</form>


<div>
    <button type="button" class="btn green">  @Html.ActionLink("Back to List", "Index", null, new { @style = "color:#FFFFFF;" }) <span class=""></span></button>

</div>


            return View();


    }

I even tried to use Request.Form[] method but I cannot understand why is this not binding the data properly and passing to the controller? roles in the controller param is always null.

10
  • 1
    You cannot use CheckBoxFor() on a bool? (you are binding to a proeprty named Accounts.Value which does not exist). You need to either make the property bool or use EditorFor() (which will create a dropdownlist with 3 values for null, true and false Commented Mar 10, 2018 at 11:06
  • And as a side note, you NEVER set the checked attribute when using the CheckBoxFor() method Commented Mar 10, 2018 at 11:07
  • @StephenMuecke You need to either make the property bool or use EditorFor() (which will create a dropdownlist with 3 values for null, true and false it is already bool, no? plus I even tried to use editfor() ` @Html.EditorFor(model => model.Accounts.Value)` no luck Commented Mar 10, 2018 at 11:10
  • okay unchecked. but the problem persists. Commented Mar 10, 2018 at 11:10
  • Its @Html.EditorFor(model => model.Account) Commented Mar 10, 2018 at 11:11

2 Answers 2

1

Working in ASP.Net can be confusing for a simple reason:

No mater how much it might look like a Desktop application, it is still a 1980 HTML WebFormular. All the old rules and design decisions are still there.

The Commentors are trying to tell you something like this:

If you leave the creation/mapping of your BackgroundClasses to HTML to some automatic, you can not use nullable bools. The HTML Checkbox does not have 3-state support. It comes from a time, when 3 state support was not yet a thing. It is a simple old 2 State Checkbox. It shares that property with the old Windows Forms Checkbox.

The closest HTML mapping if you have 3+ states is a Dropdown list. So either:

  • stop using nullable bools
  • stop trying to use a checkbox
Sign up to request clarification or add additional context in comments.

Comments

0

As already mentioned in the comments above, You cannot use CheckBoxFor() on a bool? What I would suggest is to change your model class to;

public partial class Role
{
    public int Role_Id { get; set; }
    public int EmpID { get; set; }
    public bool Door_Unlock { get; set; }
    public bool Accounts { get; set; }
    public bool Bounds_Email { get; set; }
    public bool Salary_Privilege { get; set; }
    public bool Card_Acceptance { get; set; }
    public bool IsAdmin { get; set; }

    public virtual Employee Employee { get; set; }
}

and your controller remains the same;

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "EmpID,Door_Unlock,Accounts,Bounds_Email,Salary_Privilege,Card_Acceptance,IsAdmin")] Role roles)
        {
 if (ModelState.IsValid)
                {

                    //db.Roles.Add(roles);
                   // db.SaveChanges();
                    return RedirectToAction("Index");
                }

and your Create.cshtml;

@model granjurEPS.Models.Role

@{
    ViewBag.Title = "User Role";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>New User Role</h2>
@*@using (Html.BeginForm())*@
<form method="POST">


    @Html.AntiForgeryToken()
<div class="form-horizontal">
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.FirstName, "Select Employee", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("EnrollNumber", null, "-Select Employee-", htmlAttributes: new { id = "ddEnrollNumber", @class = "form-control" })


            @Html.ValidationMessageFor(model => model.Employee.FirstName, "", new { @class = "text-danger" })

        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Accounts, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor( model => model.Accounts, new { name = "accountsCheck", @class = "accountsCheck" })
            @*@Html.EditorFor(model => model.Accounts.Value)*@

            @Html.ValidationMessageFor(model => model.Accounts, "", new { @class = "text-danger" })

        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Bounds_Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Bounds_Email, new { @class = "boundsCheck" })
            @Html.ValidationMessageFor(model => model.Bounds_Email, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Card_Acceptance, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Card_Acceptance, new { @class = "cardCheck" })
            @Html.ValidationMessageFor(model => model.Card_Acceptance, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Door_Unlock, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Door_Unlock, new { @class = "doorUnlockCheck" })
            @Html.ValidationMessageFor(model => model.Door_Unlock, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Salary_Privilege, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Salary_Privilege, new { @class = "salaryCheck" })
            @Html.ValidationMessageFor(model => model.Salary_Privilege, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.IsAdmin, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.IsAdmin, new { @class = "isAdminCheck" })
            @Html.ValidationMessageFor(model => model.IsAdmin, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Grant Permissions" class="btn green" />
        </div>
    </div>
    </div>
</form>


<div>
    <button type="button" class="btn green">  @Html.ActionLink("Back to List", "Index", null, new { @style = "color:#FFFFFF;" }) <span class=""></span></button>

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.