views:

46

answers:

1

I have a weird problem with EditorFor in one of my views. The following code throws an ArgumentNullException.

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

However, the following code is fine.

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

Model.Name is a string variable - and it's set.

EditorFor works in another view - until this view crashes, at which point then I have to restart the development web server (Cassini) or all the EditorFor calls crash with the same message.

I ran a test with the MVC 2 source, hoping I could get some insight, but that worked OK! Presumably the MVC 2 RTM source on there should be the one in VS2010 (dates seem to tie in).

I've just switched to TextBoxFor, as that's fine - but I'd like to know what the issue is. I'm running under .NET 4, VS2010 and Win7 x64.

[Update: same issue occurs with DisplayFor.]

Partial stack trace is:

[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  A: 

What does the Editor template look like for the property you are passing? (I assume x.Name is a string, so you should have a template called String.ascx in Views/Shared/EditorTemplates).

It likely the editor template that is getting loaded for string does not like a null string being passed.

If you don't have a custom editor template for string (and all you want is a basic textbox), then there is no reason to use EditorFor.

James Connell
Yes, "Name" is a string variable. And no, I don't have a custom editor. Guess I'll just skip EditorFor, but was interested in why it breaks in some scenarios (and DisplayFor also seems to do). Maybe it'll go way with MVC 3...
dommer
Yeah, its probably a bug in hot it handles the default EditorFor string template. I bet it just calls Html.TextBox internally when there is no custom template.
James Connell