简体   繁体   English

从“ System.String”类型到“ System.Boolean”类型的参数转换失败

[英]The parameter conversion from type 'System.String' to type 'System.Boolean' failed

I am getting below error in MVC on Checkbox after validation. 验证后,我在Checkbox上的MVC中遇到错误。 I have check that the property have value as True but i am still getting same error. 我已经检查过该属性的值为True,但是仍然出现相同的错误。

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

Exception : 例外情况:

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:

First make sure that the IsCardForSaving property is boolean and not string: 首先,确保IsCardForSaving属性为布尔值而不是字符串:

public bool IsCardForSaving { get; set; }

I have check that the property have value as True 我检查该属性的值为True

The correct value for a boolean field in C# is true , not True . C#中布尔字段的正确值是true ,而不是True


UPDATE: 更新:

Now that you have shown the exception stacktrace it seems that you have another input field inside your view with the same name IsCardForSaving . 现在,您已经显示了异常stacktrace,看来您的视图内还有另一个名称为IsCardForSaving输入字段。 The CheckBoxFor helper generates an additional hidden field with the same name but that is alright because it has the value false. CheckBoxFor帮助程序会生成一个具有相同名称的其他隐藏字段,但是可以,因为它的值为false,这是可以的。 This is needed because in HTML only if you check the checkbox, its value will be sent to the server. 这是必需的,因为仅在HTML中,如果选中此复选框,其值将发送到服务器。 So you get this: 所以你得到这个:

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

That's fine. 没关系。 Now look for some other input element, except those 2 with the same name="IsCardForSaving" . 现在寻找其他输入元素,除了那些两个具有相同name="IsCardForSaving"

Edit: 编辑:

Checkbox helper automatically emit an additional hidden field with false value. 复选框帮助程序会自动发出一个其他带有假值的隐藏字段。

So if checkbox is checked browser will send two values for this checkbox, "1" and false(1 takes precedence for non array type), otherwise it will only send false. 因此,如果选中此复选框,浏览器将为此复选框发送两个值:“ 1”和false(非数组类型优先于1),否则它将仅发送false。

:

1) Change your property name 1)更改您的财产名称

2) another way of resolving this issue ( that I have discovered to be a good practice ) is to avoid "not-nullable" fields in the Model. 2)解决此问题的另一种方法( 我发现这是一种很好的做法 ),是避免在模型中使用“不可为空”的字段。

3) Checkout the same problem : The parameter conversion from type 'System.String' to type ''X' failed because no type converter can convert between these types 3)签出相同的问题: 从类型'System.String'到类型'X'的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换

Update: 更新:

More solutions 更多解决方案

asp.net mvc checkbox inconsistency asp.net mvc复选框不一致

Error msg: Failed to convert parameter value from a String to a Boolean 错误消息:无法将参数值从字符串转换为布尔值

http://mvc3withrazorviewengine.blogspot.in/ http://mvc3withrazorviewengine.blogspot.in/

The problem is the name of your action parameter 问题是您的操作参数的名称

public ActionResult Deneme(DenemeModel model)

Change it to 更改为

public ActionResult Deneme(DenemeModel request)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从类型“System.String”到类型“System.Web.Mvc.SelectListItem”的参数转换失败 - The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed linq查询无法将类型为“ System.Boolean”的对象转换为类型为“ System.String”的对象 - linq query getting Unable to cast object of type 'System.Boolean' to type 'System.String' 无法将类型为“ System.Boolean”的对象转换为类型为“ System.String”的对象 - Unable to Cast Object of type 'System.Boolean' to type 'System.String' 无法将“System.String”类型的 object 转换为“System.Boolean.Net Core MVC” - Unable to cast object of type 'System.String' to type 'System.Boolean .Net Core MVC C# MVC HttpPost 失败 – ViewModel ModelState 问题(来自类型“System.String”的参数转换...) - C# MVC HttpPost Failed – ViewModel ModelState Issue (The parameter conversion from type 'System.String'…) MVC数据库首先出现存储过程错误无法将类型为“ System.Boolean”的对象转换为类型为“ System.String”的对象 - MVC Database first with stored procedure error Unable to cast object of type 'System.Boolean' to type 'System.String' 从类型'System.String'到类型''Y'的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换 - The parameter conversion from type 'System.String' to type ''Y' failed because no type converter can convert between these types 从类型&#39;System.String&#39;到类型&#39;&#39;X&#39;的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换 - The parameter conversion from type 'System.String' to type ''X' failed because no type converter can convert between these types @ Html.DropDownListFor绑定给出“从类型&#39;System.String&#39;到类型的参数转换”错误 - @Html.DropDownListFor binding giving “The parameter conversion from type 'System.String' to type” error 强制转换为值类型System.Boolean,因为物化值为null,结果类型的泛型参数必须使用可为空的类型 - The cast to value type System.Boolean failed because the materialized value is null, result type's generic parameter must use a nullable type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM