复选框始终返回False MVC5

时间:2018-10-09 07:42:17

标签: asp.net asp.net-mvc razor asp.net-mvc-5

我在表单中使用复选框和单选按钮,我可以获取除复选框以外的所有值,它始终返回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"@Checked=true,但是它不起作用,每当我提交表单时,它都会返回False。 由于我是MVC和Razor的新手,所以无法获得它。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-2)

我刚刚更新了“查看复选框”和“单选按钮”代码,现在可以正常使用了。

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

 <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>