本文介绍了将复选框值插入数据库ASP.NET MVC,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨朋友我会告诉你我的问题现在我有这个模型 公共课管理员 { [Key] public int ID {获得; set ; } [必需,MaxLength( 400 )] public string LoginName { get ; set ; } public bool ISEnroled { get ; set ; } } 这是我的控制器 public ActionResult Admins(Admin rec, bool ISEnroled) var dbrec = db.Admins.Find(rec.ID); dbrec.LoginName = rec.LoginName; dbrec.ISEnroled = ISEnroled; db.SaveChanges(); { 这是视图 < script> 函数submitForm(){< pre> $( #formParentID)提交()。 } < / script> < form action = @ Url.Action( Admins) id = formParentID target = _ self method = post enctype = multipar t / form-data > @ Html.Hidden(ID,Model.ID) < div class = blog_form_input > < label > 登录名< / label > @ Html.TextBox(LoginName,Model.LoginName,new {@class =name_input}) < span id = errorLoginName style = display:none > < label style = color:red > 您必须输入登录名< / label > < / span > < / div > < div class = blog_form_input > < label > 注册< / label > @ {string ISEnroled = Model.ISEnroled? checked:;} < input 类型 = 复选框 名称 = ISEnroled id = @ string.Format( ISEnroled + 模型。 ISEnroled) class = css-checkbox9 all_check value = @ Model.ISEnroled @ ISEnroled / > < label for = @ string.Format( ISEnroled + Model.ISEnroled) class = css-label9 已检查 = 已选中 > & nbsp; < / label > < / div > 当我点击复选框传递它没有通过的真值时这里的问题和 将它保存为真值数据库我不知道为什么即使它是假值也没有传递到保存在数据库中 我有什么问题我有完成了? 我尝试了什么: 我试过用上面的代码,但我无法配置错误解决方案 (#formParentID)。submit(); } < / script> < form action = @ Url.Action( Admins) id = formParentID target = _ self method = post enctype = multipar t / form-data > @ Html.Hidden(ID,Model.ID) < div class = blog_form_input > < label > 登录名< / label > @ Html.TextBox(LoginName,Model.LoginName,new {@class =name_input}) < span id = errorLoginName style = display:none > < label style = color:red > 您必须输入登录名< / label > < / span > < / div > < div class = blog_form_input > < label > 注册< / label > @ {string ISEnroled = Model.ISEnroled? checked:;} < input 类型 = 复选框 名称 = ISEnroled id = @ string.Format( ISEnroled + 模型。 ISEnroled) class = css-checkbox9 all_check value = @ Model.ISEnroled @ ISEnroled / > < label for = @ string.Format( ISEnroled + Model.ISEnroled) class = css-label9 已检查 = 已选中 > & nbsp; < / label > < / div > 当我点击复选框传递它没有通过的真值时这里的问题和 将它保存为真值数据库我不知道为什么即使它是假值也没有传递到保存在数据库中 我有什么问题我有完成了? 我尝试了什么: 我试过用上面的代码,但我无法配置错误 Quote: < input type = 复选框 name = ISEnroled ... value = @ Model.ISEnroled @ ISEnroled / > ; 您将复选框的值设置为当前值来自模型, false 。 所以即使你选中复选框,当你提交表格时,你提交的价值是假。 使用 CheckBox [ ^ ]或CheckBoxFor [ ^ ]帮助者渲染复选框: < div class = blog_form_input > @ Html.LabelFor(m => m.LoginName,登录名) @ Html.TextBoxFor(m => m.LoginName,new {@class =name_input}) < span id = errorLoginName 样式 = display:none > < label 样式 = 颜色:红色 > 您必须输入登录名< / label > < / span > < / div > < div class = blog_form_input > @ Html.LabelFor(m => m.ISEnroled,Enroll) @ Html.CheckBoxFor(m => m.ISEnroled,new {@class =css-checkbox9 all_check}) < label for = ISEnroled class = css-label9 已选中 = 已选中 > < / label > < / div > @ Html.LabelFor(m => m.ISEnroled,注册) < input type =checkboxname =ISEnroledid [email protected](ISEnroled+ Model。 ISEnroled)class =css-checkbox9 all_checkvalue =@ Model.ISEnroled@ISEnroled /> 并编写一个脚本来设置隐藏字段覆盖值并在表单上获取此设置隐藏字段值 Hi friends I will show you my issue Now I have this modelpublic class Admin{[Key] public int ID { get; set; }[Required, MaxLength(400) ] public string LoginName { get; set; }public bool ISEnroled { get; set; }}and this is my controllerpublic ActionResult Admins( Admin rec, bool ISEnroled)var dbrec = db.Admins.Find(rec.ID); dbrec.LoginName = rec.LoginName; dbrec.ISEnroled = ISEnroled;db.SaveChanges(); {and this is the view<script> function submitForm() { <pre> $("#formParentID").submit(); }</script><form action="@Url.Action("Admins")" id="formParentID" target="_self" method="post" enctype="multipart/form-data"> @Html.Hidden("ID", Model.ID)<div class="blog_form_input"> <label>Login Name</label> @Html.TextBox("LoginName", Model.LoginName, new { @class = "name_input" }) <span id="errorLoginName" style="display:none"> <label style="color:red">You have to enter login name </label></span> </div> <div class="blog_form_input"> <label>Enroll</label> @{ string ISEnroled = Model.ISEnroled ? "checked" : "";} <input type="checkbox" name="ISEnroled" [email protected]("ISEnroled" + Model.ISEnroled) class="css-checkbox9 all_check" value="@Model.ISEnroled" @ISEnroled /> <label [email protected]("ISEnroled" + Model.ISEnroled) class="css-label9" checked="checked"> </label> </div>the problem here when I click the checkbox to pass the true value it doesn't pass andsave it as true value in the database I don't know why even it is false value it is not passing to save in the databasewhat's wrong I have done ?What I have tried:I tried to use the code above but I couldn't configure the error 解决方案 ("#formParentID").submit(); }</script><form action="@Url.Action("Admins")" id="formParentID" target="_self" method="post" enctype="multipart/form-data"> @Html.Hidden("ID", Model.ID)<div class="blog_form_input"> <label>Login Name</label> @Html.TextBox("LoginName", Model.LoginName, new { @class = "name_input" }) <span id="errorLoginName" style="display:none"> <label style="color:red">You have to enter login name </label></span> </div> <div class="blog_form_input"> <label>Enroll</label> @{ string ISEnroled = Model.ISEnroled ? "checked" : "";} <input type="checkbox" name="ISEnroled" [email protected]("ISEnroled" + Model.ISEnroled) class="css-checkbox9 all_check" value="@Model.ISEnroled" @ISEnroled /> <label [email protected]("ISEnroled" + Model.ISEnroled) class="css-label9" checked="checked"> </label> </div>the problem here when I click the checkbox to pass the true value it doesn't pass andsave it as true value in the database I don't know why even it is false value it is not passing to save in the databasewhat's wrong I have done ?What I have tried:I tried to use the code above but I couldn't configure the errorQuote:<input type="checkbox" name="ISEnroled" ... value="@Model.ISEnroled" @ISEnroled />You're setting the value of the checkbox to the current value from the model, which is false.So even if you check the checkbox, when you submit the form, the value you're submitting is "false".Use either the CheckBox[^] or CheckBoxFor[^] helpers to render the checkbox:<div class="blog_form_input"> @Html.LabelFor(m => m.LoginName, "Login Name") @Html.TextBoxFor(m => m.LoginName, new { @class = "name_input" }) <span id="errorLoginName" style="display:none"> <label style="color:red">You have to enter login name </label></span></div><div class="blog_form_input"> @Html.LabelFor(m => m.ISEnroled, "Enroll") @Html.CheckBoxFor(m => m.ISEnroled, new { @class = "css-checkbox9 all_check" }) <label for="ISEnroled" class="css-label9" checked="checked"> </label></div>We can achieve it by setting hidden field value and get it's value on the form post @Html.LabelFor(m => m.ISEnroled, "Enroll") <input type="checkbox" name="ISEnroled" [email protected]("ISEnroled" + Model.ISEnroled) class="css-checkbox9 all_check" value="@Model.ISEnroled" @ISEnroled />and write a script to set hidden fields overwrite value and get this set hidden field value on the form post 这篇关于将复选框值插入数据库ASP.NET MVC,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 10:01