{"action":"created","issue":{"url":"https://api.github.com/repos/mcintyre321/FormFactory/issues/32","repository_url":"https://api.github.com/repos/mcintyre321/FormFactory","labels_url":"https://api.github.com/repos/mcintyre321/FormFactory/issues/32/labels{/name}","comments_url":"https://api.github.com/repos/mcintyre321/FormFactory/issues/32/comments","events_url":"https://api.github.com/repos/mcintyre321/FormFactory/issues/32/events","html_url":"https://github.com/mcintyre321/FormFactory/issues/32","id":188963808,"number":32,"title":"NullReferenceException while returning invalid vievmodel","user":{"login":"mikolajtr","id":9322616,"avatar_url":"https://avatars.githubusercontent.com/u/9322616?v=3","gravatar_id":"","url":"https://api.github.com/users/mikolajtr","html_url":"https://github.com/mikolajtr","followers_url":"https://api.github.com/users/mikolajtr/followers","following_url":"https://api.github.com/users/mikolajtr/following{/other_user}","gists_url":"https://api.github.com/users/mikolajtr/gists{/gist_id}","starred_url":"https://api.github.com/users/mikolajtr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikolajtr/subscriptions","organizations_url":"https://api.github.com/users/mikolajtr/orgs","repos_url":"https://api.github.com/users/mikolajtr/repos","events_url":"https://api.github.com/users/mikolajtr/events{/privacy}","received_events_url":"https://api.github.com/users/mikolajtr/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-11-13T10:46:03Z","updated_at":"2016-11-14T09:48:45Z","closed_at":null,"body":"When viewmodel is passed again to form as invalid (ModelState.IsValid == false), form in my Razor view throws System.NullReferenceException.\r\n\r\nMy viewmodel\r\n```\r\npublic class CategoryViewModel\r\n    {\r\n        public CategoryViewModel()\r\n        {\r\n            CategoryProperties = new List<CategoryPropertyViewModel>();\r\n        }\r\n\r\n        [Hidden]\r\n        public int Id { get; set; }\r\n\r\n        [Required]\r\n        [MaxLength(100)]\r\n        [DisplayName(\"Category name\")]\r\n        public string Name { get; set; }\r\n        \r\n        [DisplayName(\"Base category\")]\r\n        public int BaseCategoryId { get; set; }\r\n\r\n        public IEnumerable<int> BaseCategoryId_choices()\r\n        {\r\n            var categoryService = CategoryService.GetService();\r\n            var data = categoryService.GetCategories();\r\n\r\n            return data.Select(x => x.Id.DisplayName(x.Name));\r\n        }\r\n\r\n        public ICollection<CategoryPropertyViewModel> CategoryProperties { get; set; } \r\n    }\r\n\r\npublic class CategoryPropertyViewModel\r\n    {\r\n        [Hidden]\r\n        public int Id { get; set; }\r\n\r\n        [Required]\r\n        [MaxLength(100)]\r\n        [DisplayName(\"Property name\")]\r\n        public string Name { get; set; }\r\n\r\n        [Required]\r\n        [DisplayName(\"Property type\")]\r\n        public PropertyType PropertyType { get; set; }\r\n\r\n        [Hidden]\r\n        public int CategoryId { get; set; }\r\n    }\r\n```\r\nMy Razor view:\r\n```\r\n@using FormFactory\r\n@using FormFactory.AspMvc\r\n@model Reservations.DataUI.Models.CategoryViewModel\r\n\r\n<h2>@ViewData[\"Title\"]</h2>\r\n\r\n@using(Html.BeginForm(ViewData[\"Action\"].ToString(), \"Categories\", FormMethod.Post, new {@class = \"form-horizontal\", role = \"form\"}))\r\n{\r\n    @FF.PropertiesFor(Model).Render(Html)\r\n\r\n    <button type=\"submit\" class=\"btn btn-info\">Save</button>\r\n}\r\n```\r\n\r\nMy POST controller method:\r\n```\r\n[HttpPost]\r\npublic ActionResult Add(CategoryViewModel model)\r\n{\r\n    if (!ModelState.IsValid)\r\n    {\r\n        return View(\"Update\", model);\r\n    }\r\n    return RedirectToAction(\"Index\");\r\n}\r\n```\r\n\r\nMy GET controller method:\r\n```\r\npublic ActionResult Add()\r\n{\r\n    ViewData[\"Title\"] = \"Add category\";\r\n    ViewData[\"Action\"] = \"Add\";\r\n    var model = new CategoryViewModel();\r\n    return View(\"Update\", model);\r\n}\r\n```\r\n\r\nStack trace:\r\n```\r\nASP._Page_Views_Categories_Update_cshtml.Execute() in C:\\Users\\trawinski\\Documents\\reservations_clean\\Reservations.DataUI\\Views\\Categories\\Update.cshtml:7\r\n   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +270\r\n   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +122\r\n   System.Web.WebPages.StartPage.RunPage() +63\r\n   System.Web.WebPages.StartPage.ExecutePageHierarchy() +100\r\n   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131\r\n   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695\r\n   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382\r\n   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431\r\n   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39\r\n   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116\r\n   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529\r\n   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106\r\n   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321\r\n   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185\r\n   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42\r\n   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133\r\n   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56\r\n   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40\r\n   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34\r\n   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70\r\n   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133\r\n   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56\r\n   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37\r\n   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44\r\n   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39\r\n   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62\r\n   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133\r\n   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56\r\n   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37\r\n   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39\r\n   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39\r\n   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39\r\n   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70\r\n   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133\r\n   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56\r\n   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37\r\n   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40\r\n   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38\r\n   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9765121\r\n   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155\r\n```"},"comment":{"url":"https://api.github.com/repos/mcintyre321/FormFactory/issues/comments/260291504","html_url":"https://github.com/mcintyre321/FormFactory/issues/32#issuecomment-260291504","issue_url":"https://api.github.com/repos/mcintyre321/FormFactory/issues/32","id":260291504,"user":{"login":"mcintyre321","id":275706,"avatar_url":"https://avatars.githubusercontent.com/u/275706?v=3","gravatar_id":"","url":"https://api.github.com/users/mcintyre321","html_url":"https://github.com/mcintyre321","followers_url":"https://api.github.com/users/mcintyre321/followers","following_url":"https://api.github.com/users/mcintyre321/following{/other_user}","gists_url":"https://api.github.com/users/mcintyre321/gists{/gist_id}","starred_url":"https://api.github.com/users/mcintyre321/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mcintyre321/subscriptions","organizations_url":"https://api.github.com/users/mcintyre321/orgs","repos_url":"https://api.github.com/users/mcintyre321/repos","events_url":"https://api.github.com/users/mcintyre321/events{/privacy}","received_events_url":"https://api.github.com/users/mcintyre321/received_events","type":"User","site_admin":false},"created_at":"2016-11-14T09:48:45Z","updated_at":"2016-11-14T09:48:45Z","body":"So I suspect the POSTed `model` is null, which is then being reflected over, causing the NRE.\r\n\r\nCan you use Fiddler/Chrome Dev tools to see what is being POSTed?"}}
{"actor":{"display_login":"mcintyre321"}}