[英]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.