The above is supposed to insert 'Y' if a check box is checked and 'N' is its unchecked but it inserts a null value. Any one who can help with this error? Any help wil be appreciated.
you code will only work in the unhecked case. in the checked case it posts "true, false". following your current pattern of passing all elemens as parameters, you sholud should add bool parameters for the checkboxes and not use the form collection.
breemstone
Member
8 Points
27 Posts
MVC Checkbox
Apr 26, 2012 01:32 PM|LINK
I have the following code in my action control.
[HttpPost]
public ActionResult Create(FormCollection formControls, FxShipper shipper, string Shipper_USerName, string Shipper_Name, string Shipper_Phone, string Shipper_Email, string Shipper_Email_Alert_Bid, string Shipper_SMS_Alert_Bid)
{
FxShipper objShipper = new FxShipper();
objShipper.Shipper_USerName = Shipper_USerName;
objShipper.Shipper_Name = Shipper_Name;
objShipper.Shipper_Phone = Shipper_Phone;
objShipper.Shipper_Email = Shipper_Email;
if (Convert.ToBoolean(formControls["Shipper_SMS_Alert_Bid"]) == true)
//if (Convert.ToBoolean(formControls["Shipper_SMS_Alert_Bid"]) == true)
{
objShipper.Shipper_SMS_Alert_Bid = 'Y';
}
else
{
objShipper.Shipper_SMS_Alert_Bid = 'N';
}
if (Convert.ToBoolean(formControls["<Shipper_Email_Alert_Bid>"]) == true)
{
objShipper.Shipper_Email_Alert_Bid = 'Y';
}
else
{
objShipper.Shipper_Email_Alert_Bid = 'N';
}
dcfx.FxShippers.InsertOnSubmit(shipper);
dcfx.SubmitChanges();
return RedirectToAction("Index");
}
The above is supposed to insert 'Y' if a check box is checked and 'N' is its unchecked but it inserts a null value. Any one who can help with this error? Any help wil be appreciated.
jypelton
Member
184 Points
161 Posts
Re: MVC Checkbox
Apr 26, 2012 01:37 PM|LINK
If Shipper_SMS_Alert_Bid is a boolean, I imagine you should be setting it to true/false or 1/0, not a string value of 'Y'/'N'
ignatandrei
All-Star
135174 Points
21682 Posts
Moderator
MVP
Re: MVC Checkbox
Apr 26, 2012 01:40 PM|LINK
use Html.Checkbox and put a bool Shipper_SMS_Alert_Bid in the action.
More , I do not understand how it inserts a null when you have
if (condition)
{
objShipper.Shipper_Email_Alert_Bid = 'Y';
}
else
{
objShipper.Shipper_Email_Alert_Bid = 'N';
}
Did you recompile?
breemstone
Member
8 Points
27 Posts
Re: MVC Checkbox
Apr 26, 2012 01:57 PM|LINK
When I use the html.checkbox and put a bool in the action the following line brings the following error:
Line - if (Convert.ToBoolean(formControls["Shipper_SMS_Alert_Bid"]) == true)
Error-String was not recognized as a valid Boolean.
ignatandrei
All-Star
135174 Points
21682 Posts
Moderator
MVP
Re: MVC Checkbox
Apr 26, 2012 02:01 PM|LINK
I said
Put html.checkbox AND a bool in the action with the same name as the checkbox name
bruce (sqlwo...
All-Star
36882 Points
5451 Posts
Re: MVC Checkbox
Apr 26, 2012 03:42 PM|LINK
you code will only work in the unhecked case. in the checked case it posts "true, false". following your current pattern of passing all elemens as parameters, you sholud should add bool parameters for the checkboxes and not use the form collection.