TypecastException/AspNetIdentity2GroupPermissions

Error / exception in User Admin View, unable to create Users

Opened this issue · 2 comments

Hi, I wanted to report a bug, there is an exception in the user create view / action

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

            Line 35:         </label>
            Line 36:         <div class="col-md-10">
            Line 37:             @foreach (var item in (SelectList)ViewBag.GroupsList)
            Line 38:             {
            Line 39:           <div>

Source File: g:\AspMvcIdentity\AspNetIdentity2GroupPermissions\AspNetIdentity2GroupPermissions\Views\UsersAdmin\Create.cshtml Line: 37

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
ASP._Page_Views_UsersAdmin_Create_cshtml.Execute() in g:\AspMvcIdentity\AspNetIdentity2GroupPermissions\AspNetIdentity2GroupPermissions\Views\UsersAdmin\Create.cshtml:37
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +270
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +122
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) +695
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +106 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42

This fix for this is to change the create action a little.

Add a function called AddErrors:
private void AddErrors(IdentityResult result) { foreach (var error in result.Errors) { ModelState.AddModelError("", error); } }

Then in the UserAdminController Create action after the close } of the if(adminresult.Succeeded) add
AddErrors(adminresult);

And finally change the code at the bottom just before the return, to:

ViewBag.GroupsList = new SelectList(this.GroupManager.Groups, "Id", "Name");

papyr commented

Can you paste a rawbin of the update snippet/class here please