我已经读过你可以在Razor中为你的_Layout使用ViewModel,但我还没有看到任何例子......
在_Layout的顶部,我有......
@inherits System.Web.Mvc.WebViewPage<CAPPortal.ViewModels.MasterViewModel>
但每次我尝试访问其中一个属性时都会收到错误..
System.NullReferenceException:未将对象引用设置为对象的实例。
Line 23: @Model.Foobar
例外:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP._Layout_cshtml.Execute() in d:\Visual Studio 2010\CAPPortal\CAPPortal\Views\Shared\_Layout.cshtml:23
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +172
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +86
System.Web.WebPages.<>c__DisplayClass7.<RenderPage>b__6(TextWriter writer) +98
System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +12
System.Web.WebPages.WebPageBase.Write(HelperResult result) +67
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 b ody) +67
System.Web.WebPages.WebPageBase.PopContext() +262
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +93
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
答案 0 :(得分:2)
请确保在渲染视图时传入实际的模型对象。例如:
public ActionResult ShowStuff() {
return View(new MyModelType());
}
如果你没有传入模型对象,那么Model
属性确实是null,并且在视图中尝试访问它的属性当然会抛出NullReferenceException
。