0

我有一个使用带有剃刀的 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)

为什么会这样?

笔记:

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

4

2 回答 2

1

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

解决此问题的一种方法(我发现这是一种很好的做法)是避免模型中的“不可为空”字段。

于 2012-08-27T11:31:42.500 回答
0

试试这个

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

并为这些定义 chkHtmlAttributes

tabindex = "4", style = "width:15px"
于 2012-04-13T14:25:18.273 回答