0

在我的一个观点中,我对 EditorFor 有一个奇怪的问题。以下代码引发 ArgumentNullException。

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

但是,下面的代码很好。

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

Model.Name 是一个字符串变量 - 并且已设置。

EditorFor 在另一个视图中工作 - 直到此视图崩溃,此时我必须重新启动开发 Web 服务器 (Cassini) 或所有 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
4

1 回答 1

2

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

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

如果您没有用于字符串的自定义编辑器模板(并且您想要的只是一个基本文本框),那么没有理由使用 EditorFor。

于 2010-10-31T15:36:53.757 回答