加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:45:35 所属栏目:asp.Net 来源:网络整理
导读:我有一个奇怪的问题。 我的MVC应用程序似乎工作完美,除了一个视图页面。 有问题的视图页面(组织/编辑)在页面上的每个代码项目上获得“NullReferenceException”。无论是Html.TextBoxFor()还是HTML.AntiForgeryToken()。 我有我的模型,视图和控制器在这里列
我有一个奇怪的问题。

我的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:UsersmtaylorProjectsCheck Im Heremtaylor-branchCheckImHere_v2CheckImHereMVCViewsOrganizationEdit.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#语句。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读