Matt Sieker
Matt Sieker

Reputation: 9635

Asp.Net MVC3 EditorFor Exception

This really makes no sense to me whatsoever.

I have a view model with this field in it:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime Scheduled { get; set; }

I then display this field in the view (this is an edit view) like this:

@Html.EditorFor(m=>m.Scheduled)

The first time after a clean/rebuild cycle, it works fine, it displays just the date in the box (I've done lots of fiddling lately to figure out a good way to have it not display the date and time in the box, and this seems the best way).

However, once I refresh the page, I get the following:

Value cannot be null. Parameter name: stream

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: stream

And this error persists until another clean/rebuild cycle. The stack trace seems to indicate that the view engine is attempting to load the template from the /Shared/EditorTemplates directory. I have no template for this, since the documentation seems to indicate that it should default to a text box, which is what I want.

I'm not using a straight TextBoxFor since it doesn't do the formatting, and EditorFor seems to do that. And when I try to make a template to satisfy this exception, I'm back at the issue of trying to figure out how to format the date in the view.

So, I suppose two things will work for an answer: Why this error is happening only after the first refresh, or how to format a date inside a Razor view.

Per request, the full stack:

[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.GetCompiledType(VirtualPath virtualPath) +10 System.Web.Compilation.BuildManager.GetCompiledType(String virtualPath) +28 System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetCompiledType(String virtualPath) +7 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +58 System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) +584 System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1027 System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +66 System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper1 html, Expression1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +118 System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper1 html, Expression1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +100 System.Web.Mvc.Html.EditorExtensions.EditorFor(HtmlHelper1 html, Expression1 expression) +57 ASP._Page_Views_Visit_visit_Edit_cshtml.Execute() in d:\Code...\Views\Visit\Visit\Edit.cshtml:34 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13 System.Web.Mvc.<>c_DisplayClass1c.b_19() +23 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +260 System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +177 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 System.Web.Mvc.Controller.ExecuteCore() +116 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 System.Web.Mvc.<>c_DisplayClassb.b_5() +37 System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +21 System.Web.Mvc.Async.<>c_DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.<>c_DisplayClasse.b_d() +50 System.Web.Mvc.SecurityUtil.b_0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Upvotes: 1

Views: 1030

Answers (1)

Matt Sieker
Matt Sieker

Reputation: 9635

This makes even less sense. In an attempt to provide further details, I fired up ProcMon, and started looking at what it was trying to access on disk, if anything. I noticed it trying to read a __MVCSITEMAPPROVIDER under the templates folder, which didn't exist.

Knowing that I was using a slightly, erm, non-standard version of the MvcSiteMapProvider library (I upgraded it to MVC3 by hand), I decided to go get the latest version off of codeplex. Dropped it in, clean & rebuilt, and it works even after refreshes.

The problem is solved, but I still have no clue why it was only working on the first load after a clean & rebuild. According to procmon, it was still trying to access __MVCSITEMAPPROVIDER on the first load, but it worked.

Upvotes: 0

Related Questions