为什么EditorFor在我的ASP.NET MVC 2应用程序抛出ArgumentNullException? [英] Why is EditorFor in my ASP.NET MVC 2 application throwing ArgumentNullException?

查看:178
本文介绍了为什么EditorFor在我的ASP.NET MVC 2应用程序抛出ArgumentNullException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有EditorFor一个奇怪的问题,我的观点之一。下面code抛出ArgumentNullException。

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

但是,下面的code是罚款。

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

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

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

我跑测试与MVC 2源代码,希望我能得到一些见解,但效果还行! presumably的MVC 2 RTM来源应该有一个在VS2010(日期似乎配合)。

我刚刚切换到TextBoxFor,因为这很好 - 但我想知道是什么问题。我在.NET 4运行,VS2010和Win7 64位。

[更新:发生于DisplayFor同样的问题]

部分堆栈跟踪是:

  [ArgumentNullException:值不能为空。
参数名:流]
   System.IO.StreamReader..ctor(流流,编码编码,布尔detectEncodingFromByteOrderMarks,缓冲区大小的Int32)9496369
   System.Web.UI.TemplateParser.ParseFile(字符串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,布尔noBuild,布尔allowCrossApp,布尔allowBuildIn precompile,布尔throwIfNotFound,布尔ensureIsUpToDate)+ 320
   System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext的背景下,VirtualPath virtualPath,布尔noBuild,布尔allowCrossApp,布尔allowBuildIn precompile,布尔throwIfNotFound,布尔ensureIsUpToDate)+111
   System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath,HttpContext的背景下,布尔allowCrossApp,布尔throwIfNotFound)+125
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath,类型requiredBaseType,HttpContext的背景下,布尔allowCrossApp)+52
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(字符串virtualPath,类型requiredBaseType)+28
   System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.CreateInstanceFromVirtualPath(字符串virtualPath,类型requiredBaseType)+36
   System.Web.Mvc.WebFormView.Render(ViewContext viewContext,TextWriter的作家)+172
   System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HTML的HtmlHelper,可视数据的ViewDataDictionary,字符串TEMPLATENAME,DataBoundControlMode模式,GetViewNamesDelegate getViewNames)1154
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HTML的HtmlHelper,ModelMetadata元数据,字符串htmlFieldName,字符串TEMPLATENAME,DataBoundControlMode模式,对象additionalViewData,ExecuteTemplateDelegate executeTemplate)1626
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HTML的HtmlHelper,ModelMetadata元数据,字符串htmlFieldName,字符串TEMPLATENAME,DataBoundControlMode模式,对象additionalViewData)+86
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 HTML,防爆pression`1前pression,字符串TEMPLATENAME,字符串htmlFieldName,DataBoundControlMode模式,对象additionalViewData,TemplateHelperDelegate templateHelper)+210
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 HTML,防爆pression`1前pression,字符串TEMPLATENAME,字符串htmlFieldName,DataBoundControlMode模式,对象additionalViewData)+129
   System.Web.Mvc.Html.EditorExtensions.EditorFor(HtmlHelper`1 HTML,防爆pression`1前pression)+87


解决方案

什么是编辑模板样子的财产要传递? (我假设x.Name是一个字符串,那么你应该在视图称为String.ascx模板/共享/ EditorTemplates)。

这可能是越来越加载字符串编辑模板不喜欢被传递一个空字符串。

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

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

解决方案

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.

这篇关于为什么EditorFor在我的ASP.NET MVC 2应用程序抛出ArgumentNullException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆