我在加载页面时在ASP MVC中得到这个nullReference异常

时间:2014-03-06 06:14:10

标签: asp.net-mvc

当我尝试从我的控制器重定向到时,

获取对象引用的错误null @ {

ViewBag.Title = "MainPage";**//here i am getting error of object reference null**

}

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=App_Web_jk52xg0t
  StackTrace:
       at ASP._Page_Views_sok_sok_cshtml.Execute() in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\cbca3c55\39d693c4\App_Web_jk52xg0t.0.cs:line 0
       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)
  InnerException: 

1 个答案:

答案 0 :(得分:0)

检查您的视图绑定到的model

在视图中

@model YourModel

确保您将此模型从控制器传递到视图

在控制器中

var model=new YourModel();
 return View(model);