Javascript 单选按钮未在DB MVC4中正确保存值?
嗨,我有两个字段,每个字段包含两个单选按钮。当我选择单选按钮并试图将单选按钮的值保存在Db中,但它总是将值保存为零。它不能正常工作 这是我的字段PaymentReceived和PaymentType。这里我编写了j查询代码。如果我选择POVisit作为Payment单选按钮,则会出现PaymentReceived单选按钮;如果我选择PaymentReceived作为Yes单选按钮,则会显示PaymentType单选按钮字段;如果我选择Cash in PaymentType,则会显示相应字段否则,所有组件都将处于隐藏模式 我的视图代码Javascript 单选按钮未在DB MVC4中正确保存值?,javascript,c#,jquery,html,asp.net-mvc-4,Javascript,C#,Jquery,Html,Asp.net Mvc 4,嗨,我有两个字段,每个字段包含两个单选按钮。当我选择单选按钮并试图将单选按钮的值保存在Db中,但它总是将值保存为零。它不能正常工作 这是我的字段PaymentReceived和PaymentType。这里我编写了j查询代码。如果我选择POVisit作为Payment单选按钮,则会出现PaymentReceived单选按钮;如果我选择PaymentReceived作为Yes单选按钮,则会显示PaymentType单选按钮字段;如果我选择Cash in PaymentType,则会显示相应字段否则
<div id ="PaymentReceived">
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(model => model.PaymentReceived, new { @class = "control-label" })
@Html.RadioButton("PaymentReceived", 1) Yes
@Html.RadioButton("PaymentReceived", 0) No
</div>
</div>
</div>
<div id ="PaymentType">
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(model => model.PaymentType, new { @class = "control-label" })
@Html.RadioButton("PaymentType", 1) Cash
@Html.RadioButton("PaymentType", 0) Cheque
</div>
</div>
</div>
如果我在收到的付款中选择“是”,则在如果(visitorviewmodel.PaymentReceived==true)如果我在PaymentReceived中选择“否”,它将在if语句中将值显示为false,与付款类型相同。无论我在单选按钮中选择了什么值,它始终将值显示为false。现在,我想要的是,我想要保存的值,根据选择在单选按钮。也就是说,如果我选择“是”,则表示它将以DB形式将值保存为1,如果我选择“否”,则它必须以DB形式将值保存为0。请任何人帮我解决这个问题。很难理解你在这里干什么。如果使用
if(visitorviewmodel.PaymentReceived==true)
则表明这些属性是bool
属性,因此不能在单选按钮中使用1
和0
的值-它必须是@Html.RadioButton(“PaymentReceived”,true)是
(另一个用于假)好的,让我尝试一下,使用布尔值属性作为付款类型
更有意义-如果值是现金
和支票
,那么您的属性应该是枚举
(并将PaymentReceived
重命名为IsPaymentReceived
,更有意义)Stephen如果我更改为true和false,它将完全停止工作这是您绑定单选按钮的属性abool
?您的代码毫无意义-您认为控制器代码中的所有代码都在做什么如果该值为true
,则将其设置为true
?然后将bool
转换为bool
?
if (visitorviewmodel.PaymentReceived == true)
visitorviewmodel .PaymentReceived = true;
else
visitorviewmodel.PaymentReceived = false;
if (visitorviewmodel.PaymentType == true)
visitorviewmodel.PaymentType = true;
else
visitorviewmodel.PaymentType = false;
var VisitorsViewObj = new VisitorsForm()
{
PaymentReceived = Convert.ToBoolean(visitorviewmodel.PaymentReceived),
PaymentType= Convert.ToBoolean(visitorviewmodel.PaymentType),
};