MVC 4.0 String未被识别为有效的布尔值

时间:2013-05-14 18:24:07

标签: asp.net-mvc boolean string-formatting

我有一个razor 2.0表单,可以在本地完美运行。我已经验证了MVC,Razor和助手的产品都是最新版本。我在每个使用@Html.BeginForm不同参数的页面上都会收到此异常。只要没有使用表单助手,MVC页面似乎工作正常:

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

这个形式很简单:

@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>

我原本怀疑dll版本错误或缺失。所有MVC组件都应该在bin文件夹中,我已经验证了这一点(至少是顶级dll)

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!

2 个答案:

答案 0 :(得分:4)

这个问题来自web.config错误,具体来说,有以下两行:

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

第二行在具有无效的布尔值时被破坏,如:

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

答案 1 :(得分:0)

我遇到了同样的问题,但这是因为我忘了将模型对象传递到视图页面。

我刚过return view();

我改为return view(new ModelObject());