Adawg
Adawg

Reputation: 378

MVC 4.0 String was not recognized as a valid Boolean

I have a razor 2.0 form which works perfectly locally. I've verified that the dll's on prod for MVC, Razor, and helpers are all the latest version. I'm getting this exception on every page that uses @Html.BeginForm with varying parameters. MVC pages seem to work fine as long as no form helpers are used:

Exception:
           [FormatException: String was not recognized as a valid Boolean.]
               System.Boolean.Parse(String value) +13981920
               System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider
      provider)     +811
               System.Web.Mvc.ViewContext.ScopeGet(IDictionary`2 scope, String name, TValue 
    defaultValue) +89
               System.Web.Mvc.ScopeCache..ctor(IDictionary`2 scope) +75
               System.Web.Mvc.ScopeCache.Get(IDictionary`2 scope, HttpContextBase httpContext) +299
               System.Web.Mvc.ViewContext.GetClientValidationEnabled(IDictionary`2 scope, 
    HttpContextBase 

            httpContext) +9

               System.Web.Mvc.Html.FormExtensions.FormHelper(HtmlHelper htmlHelper, String formAction, 
    `enter code here`FormMethod method, IDictionary`2 htmlAttributes) +233
               System.Web.Mvc.Html.FormExtensions.BeginForm(HtmlHelper htmlHelper, String actionName, 
    String controllerName, FormMethod method, Object htmlAttributes) +134
               ASP._Page_Views_Portal_LoginForm_cshtml.Execute() in 
    e:\WebRoot\NW_QA_BleedHR\Views\Portal\LoginForm.cshtml:4
               System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
               System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
               System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, 
    TextWriter writer, WebPageRenderingBase startPage) +196
               System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String 
    partialViewName, Object model, ViewDataDictionary viewData) +147
               ASP._Page_Views_Portal_Welcome_cshtml.Execute() in 
    e:\WebRoot\NW_QA_BleedHR\Views\Portal\Welcome.cshtml:9
               System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
               System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
               System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
               System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, 
    TextWriter writer, WebPageRenderingBase startPage) +181
               System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
+380
               System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
+33
               System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, 
    ResultExecutingContext preContext, Func`1 continuation) +613
               System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext

    controllerContext, IList`1 filters, ActionResult actionResult) +263
               System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult

    asyncResult) +240
               System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
               System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
               System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
               System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
               System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
               System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
               System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

    +606
               System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& 
    completedSynchronously) +288

This form is dead simple:

@model GIS.Pangea.NW.Presentation.ViewModels.LoginView
<div id="DOAlogin_div">

   @using (Html.BeginForm("CandidateLogin", "account", FormMethod.Post, new { id = "loginform" }))
   {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()

        <table id="loginForm_tb">
            <tr>
                <td colspan="2" id="loginHeader_td">
                    <h3 id="loginHeader_h3">@Model.ReturningApplicantText</h3>
                </td>
            </tr>
            <tr>
                <td class="fieldName_td" id="username_td">
                    @Html.LabelFor(p=>Model.LoginDto.UserName)
                </td>
                <td class="fieldData_td" id="usernameinput_td">
                    @Html.TextBoxFor(p => Model.LoginDto.UserName, new {id="UserName",Name="UserName" })
                    @Html.ValidationMessage("UserName")
                </td>
            </tr>
            <tr>
                <td class="fieldName_td" id="password_td">
                    <label for="Password">
                        @Html.Encode(Model.PasswordText)
                    </label>
                </td>
                <td class="fieldData_td" id="passwordinput_td">
                    @Html.PasswordFor(p=>Model.LoginDto.Password,  new {id="Password",Name="Password" })
                    @Html.ValidationMessage("Password")
                </td>
            </tr>
            <tr>
                <td colspan="2" id="forgotPassword_td">
                    @Html.ActionLink("Forgot Username or Password?","ForgotPassword")
                </td>
            </tr>
            <tr>
                <td colspan="2" id="loginSubmit_td" class="submit_td">
                    <input type="submit" class="formButton" value="@Model.LoginButtonText" />
                </td>
            </tr>
            <tr>
                <td colspan="2" id="loginValidation_td">
                    @if (!String.IsNullOrEmpty(Model.LoginDto.ValidationMessage))
                    {
                        <span class="validationMessage">@Model.LoginDto.ValidationMessage</span>
                    }
                </td>
            </tr>
        </table>

   }
</div>

I originally suspected a wrong or missing version of a dll. All MVC components should be in the bin folder, and i've verified this (at least the top level dlls)

Here is my view config file:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
          <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
      </configSections>
      <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
            <add namespace="System.Web.Helpers" />
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Optimization" />
            <add namespace="System.Web.Routing" />
            <add namespace="MVCControlsToolkit.Core" />
            <add namespace="MVCControlsToolkit.Controls" />
            <add namespace="MVCControlsToolkit.Controls.Validation" />
          </namespaces>
        </pages>
      </system.web.webPages.razor>
      <appSettings>
        <add key="webpages:Enabled" value="false" />
      </appSettings>
      <system.web>
        <compilation debug="false" targetFramework="4.5">
          <assemblies>
            <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add assembly="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
          </assemblies>
        </compilation>
        <httpHandlers>
          <add path="*" verb="*" type="System.Web.HttpNotFoundHandler" />
        </httpHandlers>
        <pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <controls>
            <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
          </controls>
        </pages>
      </system.web>
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <remove name="BlockViewHandler" />
          <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
      </system.webServer>
    </configuration>


This project uses a combination of ASP pages and MVC Views, and previously was working with webforms 1.0 and MVC 2.0. The application is setup as an application under the default website. I've seen lots of variations with this same exception but most came down to syntax errors in the form helpers. This seems to be more environmentally related. Any help would be greatly appreciated!

Upvotes: 2

Views: 2447

Answers (2)

Jason Moran
Jason Moran

Reputation: 66

This problem emerged from a web.config error, specifically, the following two lines:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

The 2nd line was broken when it had an invalid boolean value like:

<add key="UnobtrusiveJavaScriptEnabled" value="none" />

Upvotes: 4

Lijo John
Lijo John

Reputation: 1

I had the same issue, but it was because I forgot to pass model object into the view page.

I just passed as return view();

I changed into return view(new ModelObject());

Upvotes: 0

Related Questions