首页 > 解决方案 > 复选框总是返回 False MVC5

问题描述

我在表单中使用复选框和单选按钮,我可以获得除复选框之外的所有值,它总是返回 False。

我的模型代码

public bool Gender { get; set; }  //checkbox

        public bool MStatus { get; set; }  //radio button
 public void Astudent(Student stu)
    {
        try
        {
            NpgsqlConnection con = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["npg"].ConnectionString);
            string stat = "";
            if(stu.MStatus==true)
            {
                stat = "M";
            }
            else
            {
                stat = "S";
            }
            string gnd = "";
            if (stu.Gender == true)
            {
                gnd = "M";
            }
            else
            {
                gnd = "F";
            }
            StringBuilder qry = new StringBuilder();
            qry.Append("select * from fldata(");
            qry.Append("(ARRAY[");
            qry.Append("'(");
            qry.Append(stu.Id);
            qry.Append(",");
            qry.Append(stu.FName);
            qry.Append(",");
            qry.Append(stu.LName);
            qry.Append(",");
            qry.Append(stu.UName);
            qry.Append(",");
            qry.Append(stu.Email);
            qry.Append(",");
            qry.Append(gnd);
            qry.Append(",");
            qry.Append(stat);
            qry.Append(",");
            qry.Append(stu.Department);
            qry.Append(")'])::ful[]");
            qry.Append(")");
            con.Open();
            NpgsqlCommand cmd = new NpgsqlCommand(qry.ToString(), con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch(Exception ex)
        {
             ex.ToString();
        }

    }

我的控制器代码

  public ActionResult Complex() // Add New EE / Student
    {


        return View();
    }


    [HttpPost]
    public ActionResult Complex(Student stu) // Add New EE / Student
    {
        Student empadd = new Student();
        empadd.Astudent(stu);
        return RedirectToAction("ComplexView");

    }

我的查看代码

    @model postgre.Models.Student
@{
    ViewBag.Title = "Complex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>AddEmployee</h2>



<section class="panel-body">
    @Html.ValidationSummary()
</section>
@using (Html.BeginForm())
{

    <p>
        First Name:
        @Html.TextBoxFor(model => model.FName, new { @class = "form-control" })
    </p>
    <p>
        Last Name:
        @Html.TextBoxFor(model => model.LName, new { @class = "form-control" })
    </p>
    <p>
        User Name:
        @Html.TextBoxFor(model => model.UName, new { @class = "form-control" })
    </p>
    <p>
        Email:
        @Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
    </p>
    <p>
        Gender:<br /> 
        @Html.CheckBoxFor(Model => Model.Gender, new { @class = "form-check-input", @name = "gender", @value = "M", @checked = true }) &nbsp;Male&nbsp;
        @Html.CheckBoxFor(Model => Model.Gender, new { @class = "form-check-input", @name = "gender", @value = "F",})&nbsp;Female&nbsp;
    </p>
    <p>
        Marital Status:<br />   
        @Html.RadioButtonFor(Model => Model.MStatus, new { @class = "form-check-input", @name = "mstat", @value = "S" })&nbsp;Single&nbsp;
        @Html.RadioButtonFor(Model => Model.MStatus, new { @class = "form-check-input", @name = "mstat", @value = "M", @select = true })&nbsp;Married&nbsp;
    </p>
    <p>
        @Html.DropDownListFor(Model => Model.Department,
                      new SelectListItem[]{
                      new SelectListItem() {Text = "Select Your Department", Value=""},
                      new SelectListItem() {Text = "IT", Value="IT"},
                      new SelectListItem() {Text = "Management", Value="Management"},
                      new SelectListItem() {Text = "Commerce", Value="Commerce"}}, new {@class="form-control" })
    </p>
    <p><input type="submit" name="add" value="Add Employee" class="btn btn-primary" /></p>

}

我尝试了很多方法,例如使用@checked="checked"or @Checked=true,但它不起作用,当我提交表单时它返回 False。由于我是 MVC 和 Razor 的新手,所以无法获得它。任何帮助将不胜感激。

标签: asp.netasp.net-mvcrazorasp.net-mvc-5

解决方案


我刚刚更新了查看复选框和单选按钮代码,现在它工作正常。

对于有同样问题的人,请查看代码,希望对他们有所帮助。

 <p>
    Gender:<br /> 
    @Html.CheckBoxFor(Model => Model.Gender, new { @class = "form-check-input"}) &nbsp;Male&nbsp;
    @Html.CheckBoxFor(Model => Model.Gender, new { @class = "form-check-input"})&nbsp;Female&nbsp;
</p>
<p>
    Marital Status:<br />   
    @Html.RadioButtonFor(Model => Model.MStatus,true, new { @class = "form-check-input"})&nbsp;Single&nbsp;
    @Html.RadioButtonFor(Model => Model.MStatus,false, new { @class = "form-check-input" })&nbsp;Married&nbsp;
</p>

推荐阅读