【问题标题】:The parameter conversion from type 'System.String' to type 'System.Boolean' failed从类型“System.String”到类型“System.Boolean”的参数转换失败
【发布时间】:2013-12-22 11:02:49
【问题描述】:

验证后,我在复选框上的 MVC 中遇到错误。我已检查该属性的值为 True 但我仍然收到相同的错误。

 @Html.CheckBoxFor(m => m.IsCardForSaving, new { @class = "savecard" })

例外:

System.InvalidOperationException was unhandled by user code   HResult=-2146233079   Message=The parameter conversion from type 'System.String' to type 'System.Boolean' failed. See the inner exception for more information.   Source=System.Web.Mvc   StackTrace:
       at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
       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, String format, 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.CheckBoxFor[TModel](HtmlHelper`1 htmlHelper, Expression`1 expression, IDictionary`2 htmlAttributes)
       at ASP._Page_Views_Payment_EditorTemplates_PaymentDetails_cshtml.Execute() in f:\FPNext-Latest\FPNext-COA\FPNext\Views\Payment\EditorTemplates\PaymentDetails.cshtml:line 82
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)
       at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate)
       at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData)
       at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData)
       at System.Web.Mvc.Html.EditorExtensions.EditorFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression)
       at ASP._Page_Views_Payment_Air_cshtml.Execute() in f:\FPNext-Latest\FPNext-COA\FPNext\Views\Payment\Air.cshtml:line 182
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.StartPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)   InnerException: System.FormatException
       HResult=-2146233033
       Message=true,false is not a valid value for Boolean.
       Source=System
       StackTrace:
            at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
            at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
       InnerException: System.FormatException
            HResult=-2146233033
            Message=String was not recognized as a valid Boolean.
            Source=mscorlib
            StackTrace:
                 at System.Boolean.Parse(String value)
                 at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
            InnerException:

【问题讨论】:

  • 你能发布你的模型类吗?

标签: c# asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

编辑:

复选框助手会自动发出一个带有 false 值的附加隐藏字段。

所以如果复选框被选中,浏览器会为这个复选框发送两个值,“1”和false(非数组类型优先为1),否则只会发送false。

1) 更改您的住宿名称

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

3) 检查同样的问题:The parameter conversion from type 'System.String' to type ''X' failed because no type converter can convert between these types

更新:

更多解决方案

asp.net mvc checkbox inconsistency

Error msg: Failed to convert parameter value from a String to a Boolean

http://mvc3withrazorviewengine.blogspot.in/

【讨论】:

  • 设置可空属性public bool? IsCardForSaving { get; set; }
  • 查看我的更新答案
  • Html.CheckBoxFor(m => m.IsCardForSaving, new { class= "savecard" }) 不会接受可为空的布尔值?
  • 转换它Html.CheckBoxFor(m =&gt; Convert.ToBoolean(m.IsCardForSaving), new { class = "savecard" })
【解决方案2】:

首先确保IsCardForSaving 属性是布尔值而不是字符串:

public bool IsCardForSaving { get; set; }

我已检查该属性的值为 True

C# 中布尔字段的正确值是 true,而不是 True


更新:

现在您已经显示了异常堆栈跟踪,您的视图中似乎还有另一个同名的输入字段IsCardForSaving。 CheckBoxFor 帮助器生成一个具有相同名称的附加隐藏字段,但这没关系,因为它的值是 false。这是必需的,因为在 HTML 中,仅当您选中复选框时,其值才会发送到服务器。所以你得到了这个:

<input type="checkbox" name="IsCardForSaving" />
<input type="hidden" name="IsCardForSaving" value="false" />

没关系。现在寻找其他一些输入元素,除了具有相同name="IsCardForSaving" 的那些 2。

【讨论】:

  • 它的布尔值和值为真。
  • 嗯,这很奇怪。你能发布完整的异常堆栈跟踪吗?
  • 您能否显示提交表单时发送到服务器的确切内容?您可以使用 Fiddler 或 Chrome 开发人员工具栏来捕获确切的内容。另请查看我更新的答案。我认为问题在于您还有其他一些具有此名称的输入字段。
  • 是的,我得到一个隐藏字段
  • Wat 是相同的解决方案
【解决方案3】:

问题是你的动作参数的名称

public ActionResult Deneme(DenemeModel model)

改成

public ActionResult Deneme(DenemeModel request)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2020-08-07
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多