Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTML帮助CheckBox不将数据传递给控制器_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# HTML帮助CheckBox不将数据传递给控制器

C# HTML帮助CheckBox不将数据传递给控制器,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我一直试图将数据从表单传递给控制器,但它给了我空值。我的模型/控制器和视图如下所示。我甚至尝试使用表单集合,但为什么不能正确绑定数据 Role.cs Create.cshtml @model xxxxx.Models.Role @{ ViewBag.Title=“创建”; Layout=“~/Views/Shared/_Layout.cshtml”; } 创造 @*@使用(Html.BeginForm())*@ @Html.AntiForgeryToken() @LabelFor(model=>

我一直试图将数据从表单传递给控制器,但它给了我空值。我的模型/控制器和视图如下所示。我甚至尝试使用表单集合,但为什么不能正确绑定数据

Role.cs

Create.cshtml

@model xxxxx.Models.Role
@{
ViewBag.Title=“创建”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创造
@*@使用(Html.BeginForm())*@
@Html.AntiForgeryToken()
@LabelFor(model=>model.Employee.FirstName,“选择雇员”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“EnrollNumber”,null,“-Select Employee-”,htmlAttributes:new{id=“ddEnrollNumber”,@class=“form control”})
@Html.ValidationMessageFor(model=>model.Employee.FirstName,“,new{@class=“text danger”})
@LabelFor(model=>model.Accounts,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.Accounts.Value,新的{name=“accountsCheck”、@class=“accountsCheck”、@checked=“checked”})
@Html.ValidationMessageFor(model=>model.Accounts,“,new{@class=“text danger”})
@LabelFor(model=>model.Bounds_电子邮件,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.Bounds_Email.Value,新的{@class=“boundsCheck”,@checked=“checked”})
@Html.ValidationMessageFor(model=>model.Bounds_Email,“,new{@class=“text danger”})
@LabelFor(model=>model.Card_接受,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.Card_Acceptance.Value,新的{@class=“cardCheck”,@checked=“checked”})
@Html.ValidationMessageFor(model=>model.Card_Acceptance,“,new{@class=“text danger”})
@LabelFor(model=>model.Door_Unlock,htmlAttributes:new{@class=“control label col-md-2”})
@CheckBoxFor(model=>model.Door_Unlock.Value,新的{@class=“doorUnlockCheck”,@checked=“checked”})
@Html.ValidationMessageFor(model=>model.Door_Unlock,“,new{@class=“text danger”})
@LabelFor(model=>model.Salary_特权,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.Salary_Privilege.Value,新的{@class=“salaryCheck”,@checked=“checked”})
@Html.ValidationMessageFor(model=>model.Salary_Privilege,“,new{@class=“text danger”})
@LabelFor(model=>model.IsAdmin,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.IsAdmin.Value,新的{@class=“IsAdmin检查”,@checked=“checked”})
@Html.ValidationMessageFor(model=>model.IsAdmin,“,new{@class=“text danger”})
@ActionLink(“返回列表”,“索引”,空,新的{@style=“color:#FFFFFF;”})
返回视图();
}

我甚至尝试使用Request.Form[]方法,但我无法理解为什么这不能正确绑定数据并传递给控制器<控制器参数中的代码>角色始终为空。

在ASP.Net中工作可能会令人困惑,原因很简单:

无论它看起来有多像桌面应用程序,它仍然是1980年的HTML WebFormular。所有的旧规则和设计决策仍然存在

评论者试图告诉你这样的事情:

如果将BackgroundClass到HTML的创建/映射保留为某种自动操作,则不能使用可空布尔值。HTML复选框没有三态支持。它来自一个时代,当时3个州的支持还不是一件事。这是一个简单的旧2状态复选框。它与老年人共享这一财产

如果有3+个状态,则最接近的HTML映射是下拉列表。因此,要么:

  • 停止使用可为空的布尔
  • 停止尝试使用复选框

如上所述,您不能在
bool?
上使用
CheckBoxFor()
,我建议您将模型类更改为

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; }
}
你的控制器保持不变

[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 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>
@model granjurEPS.Models.Role
@{
ViewBag.Title=“用户角色”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
新用户角色
@*@使用(Html.BeginForm())*@
@Html.AntiForgeryToken()
@LabelFor(model=>model.Employee.FirstName,“选择雇员”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“EnrollNumber”,null,“-Select Employee-”,htmlAttributes:new{id=“ddEnrollNumber”,@class=“form control”})
@Html.ValidationMessageFor(model=>model.Employee.FirstName,“,new{@class=“text danger”})
@LabelFor(model=>model.Accounts,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.Accounts,新的{name=“accountsCheck”,@class=“accountsCheck”})
@*@EditorFor(model=>model.Accounts.Value)*@
@Html.ValidationMessageFor(model=>model.Accounts,“,new{@class=“text danger”})
@LabelFor(model=>model.Bounds_电子邮件,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.Bounds_电子邮件,新的{@class=“boundsCheck”})
@Html.ValidationMessageFor(model=>model.Bounds_Email,“,new{@class=“text danger”})
@LabelFor(model=>model.Card_,htmlAttributes:new{@class=“control lab
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; }
}
[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");
                }
@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>