Create a customized `MyModelMetadataProvider` extending `DataAnnotationsModelMetadataProvider` with overrided `CreateMetadata` returning `null`. Register it by `ModelMetadataProviders.Current = new MyModelMetadataProvider();`. `NullReferneceException` would come from various places, if start debugging views with those htmlhelpers. e.g.
`@Html.DisplayNameFor(model => model.Name)` causes
```
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameHelper(ModelMetadata metadata, String htmlFieldName) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Html\DisplayNameExtensions.cs:57
System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameForInternal(HtmlHelper`1 html, Expression`1 expression, ModelMetadataProvider metadataProvider) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Html\DisplayNameExtensions.cs:32
System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(HtmlHelper`1 html, Expression`1 expression) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Html\DisplayNameExtensions.cs:26
ASP._Page_Views_Products_Index_cshtml.Execute() in \\tp-ms\coding\repro1530\repro1530\Views\Products\Index.cshtml:15
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() in e:\coding\aspnetwebstack\src\System.Web.Mvc\WebViewPage.cs:93
System.Web.WebPages.StartPage.RunPage() +63
System.Web.WebPages.StartPage.ExecutePageHierarchy() +100
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) in e:\coding\aspnetwebstack\src\System.Web.Mvc\RazorView.cs:81
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) in e:\coding\aspnetwebstack\src\System.Web.Mvc\BuildManagerCompiledView.cs:82
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ViewResultBase.cs:97
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:388
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:405
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:440
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:455
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncControllerActionInvoker.cs:91
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncControllerActionInvoker.cs:137
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:308
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncControllerActionInvoker.cs:249
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:877
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:353
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:91
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:918
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:850
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:353
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:91
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:908
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:815
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) in e:\coding\aspnetwebstack\src\System.Web.Mvc\MvcHandler.cs:99
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:353
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:91
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\MvcHandler.cs:135
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) in e:\coding\aspnetwebstack\src\System.Web.Mvc\MvcHandler.cs:234
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
```
The same are with other places like
`@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })` throws
at `System.Web.Mvc.Html.LabelExtensions.LabelHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String labelText, IDictionary``2 htmlAttributes)`
`@Html.EditorFor(model => model.Name)` throws
at `System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate)`
…
Most of them are from `internal` methods. Although the error message shows the exact method where the exception comes from, it does not mention directly that modelmetadata is null or overrided `CreateMetadata` gives rise to this error page, which may be a bad experience for user's debugging.
By the way, many unit tests will set it return null.
Comments: We want nicer behavior, by throwing InvalidOperationException if the user returns null?
`@Html.DisplayNameFor(model => model.Name)` causes
```
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameHelper(ModelMetadata metadata, String htmlFieldName) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Html\DisplayNameExtensions.cs:57
System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameForInternal(HtmlHelper`1 html, Expression`1 expression, ModelMetadataProvider metadataProvider) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Html\DisplayNameExtensions.cs:32
System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(HtmlHelper`1 html, Expression`1 expression) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Html\DisplayNameExtensions.cs:26
ASP._Page_Views_Products_Index_cshtml.Execute() in \\tp-ms\coding\repro1530\repro1530\Views\Products\Index.cshtml:15
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() in e:\coding\aspnetwebstack\src\System.Web.Mvc\WebViewPage.cs:93
System.Web.WebPages.StartPage.RunPage() +63
System.Web.WebPages.StartPage.ExecutePageHierarchy() +100
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) in e:\coding\aspnetwebstack\src\System.Web.Mvc\RazorView.cs:81
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) in e:\coding\aspnetwebstack\src\System.Web.Mvc\BuildManagerCompiledView.cs:82
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ViewResultBase.cs:97
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:388
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:405
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:440
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\ControllerActionInvoker.cs:455
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncControllerActionInvoker.cs:91
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncControllerActionInvoker.cs:137
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:308
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncControllerActionInvoker.cs:249
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:877
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:353
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:91
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:918
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:850
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:353
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:91
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:908
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Controller.cs:815
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) in e:\coding\aspnetwebstack\src\System.Web.Mvc\MvcHandler.cs:99
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:353
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:229
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:81
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) in e:\coding\aspnetwebstack\src\System.Web.Mvc\Async\AsyncResultWrapper.cs:91
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) in e:\coding\aspnetwebstack\src\System.Web.Mvc\MvcHandler.cs:135
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) in e:\coding\aspnetwebstack\src\System.Web.Mvc\MvcHandler.cs:234
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
```
The same are with other places like
`@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })` throws
at `System.Web.Mvc.Html.LabelExtensions.LabelHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String labelText, IDictionary``2 htmlAttributes)`
`@Html.EditorFor(model => model.Name)` throws
at `System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate)`
…
Most of them are from `internal` methods. Although the error message shows the exact method where the exception comes from, it does not mention directly that modelmetadata is null or overrided `CreateMetadata` gives rise to this error page, which may be a bad experience for user's debugging.
By the way, many unit tests will set it return null.
Comments: We want nicer behavior, by throwing InvalidOperationException if the user returns null?