ASP.NET MVC 4 - Object reference not set to an instance of an object (Dropdown list)

ltlnutan

New Member
I've just began learning and here I got some problem that bothered me whole day. I can't solve the following error:\[code\][NullReferenceException: Object reference not set to an instance of an object.]\[/code\]I have this code in View:\[code\]<div class="editor-field">@Html.DropDownListFor(model => model.DogColors.ColorId, Model.Color, new { @id = "color_id" })@Html.ValidationMessageFor(model => model.DogColors.ColorId)</div>\[/code\]And this model:\[code\]public class CreateDog{ public Dog Dog { get; set; } public DogColors DogColors { get; set; } public IEnumerable<SelectListItem> Colors { get; set; }}\[/code\]Controller:\[code\]public ActionResult Create() { var model = new CreateDog(); using (var db = new ColorsRepository()) { model.Colors = db.All.ToList().Select(x => new SelectListItem { Value = http://stackoverflow.com/questions/15712563/x.Id.ToString(), Text = x.Name }); } model.Dog = new Dog(); model.DogColors = new DogColors(); return View(model) } \[/code\]I've added two colors to database and those colors are displayed in dropdown list. Problem is in this line when I click the button to submit:\[code\]@Html.DropDownListFor(model => model.DogColors.ColorId, Model.Colors, new { @id = "color_id" })\[/code\]That exception is constantly thrown at that place. Any ideas what could be the problem?Stack Trace:\[code\][NullReferenceException: Object reference not set to an instance of an object.]ASP._Page_Views_Projects__CreateOrEdit_cshtml.Execute() in c:\Users\Admin\Documents\Visual Studio 2012\Projects\Pro\Pro.Web\Views\Dogs\_CreateOrEdit.cshtml:65System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +276System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +108System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model) +32ASP._Page_Views_Projects_Create_cshtml.Execute() in c:\Users\Admin\Documents\Visual Studio 2012\Projects\Pro\Pro.Web\Views\Dogs\Create.cshtml:14System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97System.Web.WebPages.StartPage.RunPage() +17System.Web.WebPages.StartPage.ExecutePageHierarchy() +62System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155\[/code\]
 
Back
Top