0

I want to retrieve data from IdentityRole and show it in a web page. I have seed the IdentityRoles detailes before and I'm referencing ProjectRole Model to list it down in a web page. Below is my controller page.

private ApplicationDbContext _context;

public AdminController()
{
    _context = new ApplicationDbContext();
}
public ActionResult _ListProjectRoles()
{
    var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
    var roleManager = new RoleManager<IdentityRole>(roleStore);

    var roles = roleManager.Roles.ToList();
    var vm = new List<ProjectRole>();
    roles.ForEach(item => vm.Add(
          new ProjectRole()
          {
              Id = item.Id,
              RoleName = item.Name
          }
      ));
    return View(vm);
}

Below I have added the view page

@model IEnumerable<ExpenCare.Models.ProjectRole>
@{ 
    ViewData["Title"] = "GetAllRoles";
}
<h2>Get All Roles</h2>
<p>
    <a asp-action="create">Create New</a>
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.RoleName)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.RoleName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table>

I get the NullException Error as,

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=App_Web_fdwd0zuh
  StackTrace:
       at ASP._Page_Views_Admin__ListProjectRoles_cshtml.Execute() in D:\Shamila Stuff\ExpenseCare\ReleaseStage-1.0\ExpenCare\Views\Admin\_ListProjectRoles.cshtml:line 18
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
       at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
       at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName)
       at ASP._Page_Views_Admin_Index_cshtml.Execute() in D:\Shamila Stuff\ExpenseCare\ReleaseStage-1.0\ExpenCare\Views\Admin\Index.cshtml:line 38
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.StartPage.RunPage()
       at System.Web.WebPages.StartPage.ExecutePageHierarchy()
       at System.Web.WebPages.StartPage.RunPage()
       at System.Web.WebPages.StartPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
  InnerException: 

Columns are not null and I can't find why it throws Null Exception.

Bosco
  • 1,536
  • 2
  • 13
  • 25
shamila
  • 1,280
  • 6
  • 20
  • 45

1 Answers1

0

I took a portion of your code, the table part and pointed out the part of it that might be causing the problem. though the exception is not supposed to be a null pinter

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.RoleName) 
            //this line above is not going to work because you model is a list
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RoleName)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
                @Html.ActionLink("Details", "Details", new { id=item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.Id })
            </td>
        </tr>
    }
</table>
Bosco
  • 1,536
  • 2
  • 13
  • 25
  • @shamila can you debug the `_ListProjectRoles` be sure you have roles set correctly. see if it contains data – Bosco Sep 15 '19 at 04:53
  • I tried with other models as well, it seems that I can store data but I get NullReference error when retrieving data – shamila Sep 15 '19 at 10:46