asp.net - 复选框总是返回 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 }) Male
@Html.CheckBoxFor(Model => Model.Gender, new { @class = "form-check-input", @name = "gender", @value = "F",}) Female
</p>
<p>
Marital Status:<br />
@Html.RadioButtonFor(Model => Model.MStatus, new { @class = "form-check-input", @name = "mstat", @value = "S" }) Single
@Html.RadioButtonFor(Model => Model.MStatus, new { @class = "form-check-input", @name = "mstat", @value = "M", @select = true }) Married
</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 的新手,所以无法获得它。任何帮助将不胜感激。
解决方案
我刚刚更新了查看复选框和单选按钮代码,现在它工作正常。
对于有同样问题的人,请查看代码,希望对他们有所帮助。
<p>
Gender:<br />
@Html.CheckBoxFor(Model => Model.Gender, new { @class = "form-check-input"}) Male
@Html.CheckBoxFor(Model => Model.Gender, new { @class = "form-check-input"}) Female
</p>
<p>
Marital Status:<br />
@Html.RadioButtonFor(Model => Model.MStatus,true, new { @class = "form-check-input"}) Single
@Html.RadioButtonFor(Model => Model.MStatus,false, new { @class = "form-check-input" }) Married
</p>
推荐阅读
- wordpress - WordPress在前端的自定义角色配置文件页面上添加WP_Editor的媒体按钮不起作用
- metadata - 字符串数组的 AEM querybuider 谓词
- node.js - Vaadin-Upload 不适用于 http-proxy-middleware
- sql - 将查询分组中的空值显示为零
- javascript - Express.JS,Node.JS - 每个请求范围可访问的变量
- symfony - symfony + ratchet:如何从控制器向用户发送消息
- python - MLPClassifier 的可解释性
- angularjs - 如何在我的 Angular 控制器中打印错误字符串?
- ios - 单击 UIButton 以显示 UIPickerView 并在按钮标题上选择 ID:Swift
- javascript - 获取请求不返回请求的数据