1

I've a web using asp.net MVC 3 with razor.

In one view I'm having an odd error with the checkbox helper.

Here is the razor code:

@Html.CheckBox("rememberPassword", Model.RememberPassword, new { tabindex = "4", style = "width:15px" })

The property in the model (which I set to true in the Model constructor):

public bool RememberPassword { get; set; }

And the logged error:

2012-04-13 01:20:33.334 [13   ] Error - Reference: 0413-012033-334 - Global site error, page: /es/login
System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'System.Boolean' failed. See the inner exception for more information. ---> System.FormatException: -1' is not a valid value for Boolean. ---> System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   --- End of inner exception stack trace ---
   at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
   --- End of inner exception stack trace ---
   at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
   at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)
   at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)
   at System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType)
   at System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes)
   at System.Web.Mvc.Html.InputExtensions.CheckBoxHelper(HtmlHelper htmlHelper, ModelMetadata metadata, String name, Nullable`1 isChecked, IDictionary`2 htmlAttributes)
   at System.Web.Mvc.Html.InputExtensions.CheckBox(HtmlHelper htmlHelper, String name, Boolean isChecked, Object htmlAttributes)
   at ASP._Page_Views_Login_Index_cshtml.Execute() in d:\[...]\Views\Login\Index.cshtml:line 49
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

Why is this happening?

Note:

It keeps getting odder and odder. As magically as the error started appearing (in a productive site some day without any updates or releases) not it has stopped. It's been three days without the error. However, I'd still like to know why was it.

Diego
  • 16,436
  • 26
  • 84
  • 136

2 Answers2

1

It had nothing to do with the way of creating the checkbox. In some very odd scenarios (which I had forgotten at all), this forms may be submitted (from other sites) without this parameter. In this cases the bool field was trying to obtained from the string "-1".

One way of resolving this issue (that I have discovered to be a good practice) is to avoid "not-nullable" fields in the Model.

Community
  • 1
  • 1
Diego
  • 16,436
  • 26
  • 84
  • 136
0

Just try this instead

Html.CheckBoxFor(model => model.RememberPassword , chkHtmlAttributes)

And define the chkHtmlAttributes to these

tabindex = "4", style = "width:15px"
TRR
  • 1,637
  • 2
  • 26
  • 43
  • 1
    I can try. I won't know if this works for a day or two because I can reproduce the error myself, I just found it sometimes in the logs. **However**: shouldn't both ways work the same way (even with the same helpers)?? – Diego Apr 13 '12 at 14:27
  • Also, What I really want is to understand why is this happening, not only fix it. – Diego Apr 17 '12 at 11:56
  • Can you debug and see what value Model.RememberPassword has on the view. – TRR Apr 17 '12 at 12:23
  • I can, but I cant make sure the error will happen so I don't think it will be useful. On one hand I could never reproduce the error, on the other hand now it disappeared. Any way, the type was bool... it could just be false or true, right? If you want me to debug anyway, there's no problem! – Diego Apr 17 '12 at 13:00
  • Some part of the error says " System.FormatException: -1' is not a valid value for Boolean." just not sure where -1 is getting assigned, that too for a boolean field – TRR Apr 17 '12 at 13:11
  • "Bool?" can also be null/not set -- not just true/false. – vapcguy Oct 25 '14 at 01:50
  • but when bool is null then it is false. I am having the same issue and mine happens because I have multiple checkbox that are populated from the database when the user check one and update the database on reload of the page I get the error. I have tried removing the checkbox and the input hidden filed associated to the checkbox to make sure that the reload will work like the first time but that hasn't resolved the problem. I am not sure how to proceed. anyone with a solution please share – Jack M Nov 14 '14 at 16:26