Reputation: 97
I am getting below error in MVC on Checkbox after validation. I have check that the property have value as True but i am still getting same error.
@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:
Upvotes: 0
Views: 9372
Reputation: 46
The problem is the name of your action parameter
public ActionResult Deneme(DenemeModel model)
Change it to
public ActionResult Deneme(DenemeModel request)
Upvotes: 1
Reputation: 38703
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) Change your property name
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.
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
Update:
More solutions
asp.net mvc checkbox inconsistency
Error msg: Failed to convert parameter value from a String to a Boolean
http://mvc3withrazorviewengine.blogspot.in/
Upvotes: 2
Reputation: 1039438
First make sure that the IsCardForSaving
property is boolean and not string:
public bool IsCardForSaving { get; set; }
I have check that the property have value as True
The correct value for a boolean field in C# is true
, not 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
. The CheckBoxFor helper generates an additional hidden field with the same name but that is alright because it has the value false. This is needed because in HTML only if you check the checkbox, its value will be sent to the server. 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"
.
Upvotes: 1