The string was not recognized as valid logical

FeatureEvents.bit_Activate = Convert.ToBoolean(collection["bit_Activate"]); 

bit_Activate is a flag, as if to convert it to the boolean value above, the collection comes from the formcollection variable

+4
source share
5 answers

The easiest way to do this is probably by using ASP.NET MVC ModelBinders built-in, which allow you to use the CLR as input for your action, and the MVC binds all the properties for you. The only requirement is that you (by convention) name your form elements after the properties of the input object. Google "asp.net mvc model binder" for lots of information and tutorials.

0
source

The checkbox does not appear in the Form collection unless checked, so the following will work for you:

  FeatureEvents.bit_Activate = collection.Keys.Contains["bit_activate"]; 

PS: Html helper for a checkbox, for example, Html.Checkbox is set to HORRID, it puts a hidden input with the same name to make sure the item is always in the form collection. I believe direct HTML <INPUT> works better. If you use the htm helper, you will need to parse the array to get the value.

+1
source

Have you set the clock to the value you are trying to convert to boolean?

I think that most likely you need to do something like

 FeatureEvents.bit_Activate = Convert.ToBoolean(collection["bit_Activate"].checked); 
0
source

Decision:

 FeatureEvents.bit_Activate = Convert.ToBoolean(Request.Form["bit_Activate"].Contains("true")); 
0
source

Source: https://habr.com/ru/post/1309855/


All Articles