asp.net-mvc – App_Web _ * .dll中的System.NullReferenceException

我有一个奇怪的问题。

我的MVC应用程序似乎工作完美,除了一个视图页面。

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

我有我的模型,视图和控制器在这里列出了我认为是相关的另一个问题 – http://stackoverflow.com/questions/26475866/dropdownlistfor-null-reference-error

如下所示,我的模型确实有信息。此屏幕捕获是在控制器内的“返回视图(”编辑“,型号)”中获取的。

异常详细信息

- 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,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)

视图

@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

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

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

解决方法

我找到了我的问题 here的答案

任何人找到这个:

尝试在错误后面注释下一个代码行。

@ModelType CheckImHereMVC.OrganizationEditViewModel

@Using Html.BeginForm("Edit",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

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

从链接:“有时编译器不能指出在剃刀视图中具有特定类型的错误的精确行可能是因为它不能将它们的行号保留在堆栈跟踪或某处,我发现这种情况与Null引用异常,当null为通过Url.Content。

因此,当您没有在堆栈跟踪中显示的行中没有得到任何错误时,可以检查razor视图中的下一个C#语句。

相关文章

引言 本文从Linux小白的视角, 在CentOS 7.x服务器上搭建一个...
引言: 多线程编程/异步编程非常复杂,有很多概念和工具需要...
一. 宏观概念 ASP.NET Core Middleware是在应用程序处理管道...
背景 在.Net和C#中运行异步代码相当简单,因为我们有时候需要...
HTTP基本认证 在HTTP中,HTTP基本认证(Basic Authenticatio...
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续...