代码之家  ›  专栏  ›  技术社区  ›  dommer

为什么我的ASP.NET MVC 2应用程序中的EditorFor会抛出ArgumentNullException?

  •  0
  • dommer  · 技术社区  · 15 年前

    我对EditorFor有一个奇怪的看法。以下代码引发ArgumentNullException。

    <%: Html.EditorFor(x => x.Name) %>
    

    <%: Html.TextBoxFor(x => x.Name) %>
    

    Name是一个字符串变量-它是设置好的。

    EditorFor在另一个视图中工作-直到这个视图崩溃,此时我必须重新启动development web服务器(Cassini),否则所有EditorFor调用都将崩溃,并显示相同的消息。

    我用MVC 2的源代码运行了一个测试,希望我能得到一些见解,但那没问题!估计那里的MVC 2 RTM源应该是VS2010中的一个(日期似乎是相互关联的)。

    我刚转到了textbox,因为这很好-但我想知道问题是什么。我在.NET 4、VS2010和Win7 x64下运行。

    更新 :DisplayFor也会出现同样的问题。]

    部分堆栈跟踪是:

    [ArgumentNullException: Value cannot be null.
    Parameter name: stream]
       System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) +9496369
       System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath) +232
       System.Web.UI.TemplateParser.ParseInternal() +86
       System.Web.UI.TemplateParser.Parse() +160
       System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType() +110
       System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) +65
       System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() +218
       System.Web.Compilation.BuildProvidersCompiler.PerformBuild() +40
       System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +8945798
       System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +320
       System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +111
       System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +125
       System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) +52
       System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType) +28
       System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType) +36
       System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer) +172
       System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames) +1154
       System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1626
       System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +86
       System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +210
       System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +129
       System.Web.Mvc.Html.EditorExtensions.EditorFor(HtmlHelper`1 html, Expression`1 expression) +87
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   automagic    15 年前

    您传递的属性的编辑器模板是什么样子的?(我假设x.Name是一个字符串,所以在Views/Shared/editoremplates中应该有一个名为string.ascx的模板)。

    为字符串加载的编辑器模板可能不喜欢传递空字符串。

    如果您没有字符串的自定义编辑器模板(您只需要一个基本的文本框),那么就没有理由使用editor for。