App_Web _*.dll中的System.NullReferenceException

May*_*lor 25 asp.net-mvc nullreferenceexception razor

我有一个奇怪的问题.

除了一个视图页面,我的MVC应用程序似乎工作得很好.

有问题的视图页面(组织/编辑)在页面上的每个代码项上获得'NullReferenceException'.无论是Html.TextBoxFor()还是HTML.AntiForgeryToken().

我将我的模型,视图和控制器放在另一个我认为相关的问题上 - /sf/ask/1853310651/

如下所示,我的模型内部确实有信息.此屏幕截图是在控制器内部的" 返回视图("编辑",型号) " 处拍摄的.

例外细节

- Source = App_Web_zu4jlld0
- StackTrace =    at ASP._Page_Views_Organization_Edit_vbhtml.Execute() in C:\Users\mtaylor\Projects\Check Im Here\mtaylor-branch\CheckImHere_v2\CheckImHereMVC\Views\Organization\Edit.vbhtml:line 16
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

视图

@ModelType CheckImHereMVC.OrganizationEditViewModel

@Using Html.BeginForm("Edit", "Organization", FormMethod.Post)
 @Html.AntiForgeryToken() 'get errors here
 @Html.ValidationSummary(True) 'get errors here
 @Html.TextBoxFor(Function(model) model.organizationSub.subName, New With {.class = "span12"}) 'and errors here
End Using
Run Code Online (Sandbox Code Playgroud)

我注意到的一件事是,如果我注释掉我的'textboxfor',我的错误将发生在'ValidationSummary()',如果我注释掉我的'ValidationSummary()',那么我的错误将发生在'AntiForgeryToken()'.

所以似乎错误只发生在最后一个可能的代码区域.

May*_*lor 37

我在这里找到了问题的答案

对于发现这个的人:

尝试在错误后注释掉下一个代码行.

@ModelType CheckImHereMVC.OrganizationEditViewModel

@Using Html.BeginForm("Edit", "Organization", FormMethod.Post)
   @Html.AntiForgeryToken() 
   @Html.ValidationSummary(True) 
   @Html.TextBoxFor(Function(model) model.organizationSub.subName, New With {.class = "span12"})
   @Html.TextBoxFor(Function(model) model.organizationSub.subTitle, New With {.class = "span12"})
   <img src="@Url.Content(Model.img.imgPath)" alt="IMAGES"/> 'commenting out this line fixed my issue
End Using
Run Code Online (Sandbox Code Playgroud)

在上面的情况下,我会得到错误model.organizationSub.subTitle.如果我评论该线路,我会得到错误的model.organizationSub.subName线路.然后我找到了提到的链接,并在我的所有TextBoxFors 之后注释掉了这一行.这解决了我的问题.

从链接:"有时编译器无法指向剃刀视图中具有特定类型错误的确切行可能是因为它无法将它们的行号保留在堆栈跟踪或某处.我发现这种情况为Null Reference Exception,当null为传递给Url.Content.

因此,当您在堆栈跟踪显示的行上没有出现任何错误时,检查剃刀视图中的下一个C#语句会有所帮助."

  • 我希望我能不止一次地赞成这一点.我浪费了一个小时试图理解导致我得到的NullReferenceException的原因,当我的TextBoxFor调用没有任何问题时...... (3认同)
  • 再次非常感谢!当使用.resx文件作为字符串时,剃刀模板抛出NullReference错误的问题相同,即使实际错误行是尝试在模型上使用属性的下一个.Net代码行. (2认同)