【问题标题】:Checkbox Helper: Odd string to bool conversion error复选框助手:奇数字符串到布尔转换错误
【发布时间】:2012-04-25 21:47:19
【问题描述】:

我有一个使用带有剃须刀的 asp.net MVC 3 的网络。

在一个视图中,我在使用复选框助手时遇到了一个奇怪的错误。

这是剃须刀代码:

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

模型中的属性(我在模型构造函数中设置为true):

public bool RememberPassword { get; set; }

以及记录的错误:

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)

为什么会这样?

注意:

它变得越来越奇怪。就像错误开始出现一样神奇(有一天在没有任何更新或发布的生产站点中)它并没有停止。已经三天没有错误了。不过,我还是想知道为什么会这样。

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 checkbox html-helper


    【解决方案1】:

    试试这个吧

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

    并为这些定义 chkHtmlAttributes

    tabindex = "4", style = "width:15px"
    

    【讨论】:

    • 我可以试试。我不知道这是否有效一两天,因为我可以自己重现错误,我有时只是在日志中发现它。 然而:这两种方式不应该以相同的方式工作吗(即使使用相同的助手)??
    • 另外,我真正想要的是了解为什么会发生这种情况,而不仅仅是修复它。
    • 你能调试一下看看 Model.RememberPassword 在视图上有什么值吗?
    • 我可以,但我不能确保错误会发生,所以我认为它没有用。一方面我永远无法重现错误,另一方面现在它消失了。无论如何,类型是 bool ......它可能是 false 或 true,对吗?反正你要我调试也没问题!
    • 部分错误提示“System.FormatException: -1' 不是布尔值的有效值。”只是不确定在哪里分配 -1,对于布尔字段也是如此
    【解决方案2】:

    这与创建复选框的方式无关。在一些非常奇怪的情况下(我完全忘记了),可能会在没有此参数的情况下(从其他站点)提交此表单。在这种情况下,布尔字段试图从字符串“-1”中获取。

    解决此问题的一种方法 (that I have discovered to be a good practice) 是避免模型中的“不可为空”字段。

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 2018-04-04
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2013-03-23
      • 2014-09-21
      相关资源
      最近更新 更多