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.
CheckBoxFor()on abool?(you are binding to a proeprty namedAccounts.Valuewhich does not exist). You need to either make the propertyboolor useEditorFor()(which will create a dropdownlist with 3 values fornull,trueandfalsecheckedattribute when using theCheckBoxFor()methodYou need to either make the property bool or use EditorFor() (which will create a dropdownlist with 3 values for null, true and falseit is already bool, no? plus I even tried to use editfor() ` @Html.EditorFor(model => model.Accounts.Value)` no luck@Html.EditorFor(model => model.Account)