0

我在我的一个视图中出现了一个奇怪的问题,那就是EditorFor。以下代码将引发ArgumentNullException。为什么EditorFor在ASP.NET MVC 2应用程序中抛出ArgumentNullException?

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

但是,下面的代码很好。

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

Model.Name是一个字符串变量 - 和它的设置。

另一种观点认为EditorFor工作 - 直到这种观点崩溃,在这一点,然后我不得不重新启动开发网络服务器(卡西尼)或全部EditorFor调用崩溃与相同的消息。

我跑测试与MVC 2源,希望我能得到一些见解,但效果还行!据推测,那里的MVC 2 RTM源应该是VS2010中的一个(日期似乎是配合的)。

我刚刚切换到TextBoxFor,因为这是很好 - 但我想知道是什么问题。我在.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

什么是编辑模板样子的财产要传递? (我假设x.Name是一个字符串,所以你应该在Views/Shared/EditorTemplates中有一个名为String.ascx的模板)。

它可能被传递,这是获得加载字符串不喜欢一个空字符串编辑模板。

如果你没有为字符串自定义编辑器模板(和所有你想要的是一个基本的文本框),则没有理由使用EditorFor。

+0

是的,“名称”是一个字符串变量。不,我没有自定义编辑器。猜猜我只是跳过EditorFor,但有兴趣了解为什么它在某些情况下打破(并且DisplayFor也似乎这样做)。也许它会与MVC 3一起... – dommer 2010-10-31 15:47:58

+0

是的,它可能是一个热门的bug,它处理默认的EditorFor字符串模板。我敢打赌,它只是在没有自定义模板的情况下在内部调用Html.TextBox。 – automagic 2010-10-31 17:38:58

相关问题