1

I have a View which is using Grid.MVC to display desired User Properties. My controller below, uses the User [MemberOrgID] or [SponsorOrgID] to find the all the relevant Org/Sponsor information related to that users particular ID (User will have an ID for one of the other, never both):

    // GET: Admin/UserManagement
    public async Task<ActionResult> Index()
    {
        ViewBag.headerTitle = "User Management";
        ViewBag.showCreateButton = true;
        ViewBag.createUrl = "/Admin/UserManagement/Create";
        ViewBag.createText = " Create a New User";
        List<ApplicationUser> model = UserManager.Users.ToList();
        foreach (ApplicationUser user in model)
        {
            if (user.MemberOrgId != null)
            {
                user.Organization = db.MemberOrganizations.Find(user.Organization);
            }
            //else
            //{
            //    user.Organization.Name = " ";
            //}

            if (user.SponsorOrgId != null)
            {
                user.Sponsor = db.SponsorOrganizations.Where(o => o.Id == user.SponsorOrgId).FirstOrDefault();
            }
            //else
            //{
            //    user.Sponsor.Name = " ";
            //}
        }
        return View(model);
    }

Since my first User has a Sponsor ID, this is what I can see within the Immediate Window while debugging in VS2013:

?user.Sponsor
    {System.Data.Entity.DynamicProxies.SponsorOrganizations_F2A27693F33AFF36C814F6FFA2CE63AD656938244F599B7D1AB2ED80CC26EF9A}
    [System.Data.Entity.DynamicProxies.SponsorOrganizations_F2A27693F33AFF36C814F6FFA2CE63AD656938244F599B7D1AB2ED80CC26EF9A]: {System.Data.Entity.DynamicProxies.SponsorOrganizations_F2A27693F33AFF36C814F6FFA2CE63AD656938244F599B7D1AB2ED80CC26EF9A}
    City: null
    Country: null
    Enabled: true
    FacebookUrl: "https://www.facebook.com/sponsor1"
    Id: 1
    LogoSrc: "Sponsor1.jpg"
    Name: "Sponsor1"
    OwnerId: "1d720c20-a508-4880-a5e6-80af5f2f8caf"
    OwnerName: "Sponsor Primary"
    ProfilePictureUrl: "sponsorprofilepicture/Sponsor1.jpg"
    SponsorServiceCategory: Count = 11
    State: null
    TwitterUrl: "https://twitter.com/sponsor1"
    Users: Count = 0
    WebsiteUrl: "http://www.sponsor1.com"

I am having a NullReference Issue however as when the View is loaded (below) the code o.Organization.Name is flagged as a NullReferenceException (I have added a try catch just to make things a little friendlier and let the rest of the View load):

@using GridMvc.Html
@model IEnumerable<PROJECTS.Models.ApplicationUser>

@{
    ViewBag.Title = "View Users";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml";

}

<div class="overflowPrevention">
    <!-- Grid.MVC Code -->
    <div>
        @*Images in Columns: https://gridmvc.codeplex.com/discussions/440977*@
@try
{ 
        @Html.Grid(Model).Columns(columns =>
        {
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => Html.ActionLink("Edit", "Edit", "UserManagement", new { id = o.Id }, null)).SetWidth(15);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Name).Titled("Name").Sortable(true).Filterable(true).SetWidth(100);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Position).Titled("Position").Sortable(true).Filterable(true).SetWidth(50);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Email).Titled("Email").Sortable(true).Filterable(true).SetWidth(100);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.EmailConfirmed.ToString()).Titled("Confirmed").Sortable(true).Filterable(true).SetWidth(100);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.MemberOrgId.ToString()).Titled("MemOrg.").Sortable(true).Filterable(true).SetWidth(25);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Organization.Name.ToString()).Titled("MemOrg.").Sortable(true).Filterable(true).SetWidth(25);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.SponsorOrgId.ToString()).Titled("Sponsor").Sortable(true).Filterable(true).SetWidth(25);
            columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Sponsor.Name.ToString()).Titled("Sponsor").Sortable(true).Filterable(true).SetWidth(25);
        }).WithPaging(5)
}
catch (NullReferenceException ex)
{
    return;
}
    </div>
</div>

Does anyone have thoughts on how I might negate this NullReferenceException issue? The User will always have either a MemberOrganizationID or SponsorID, meaning that either the o.Organization.Name will have a value or o.Sponsor.Name will, but never both. I'm trying to find a way to make the one which is null either not show up, or just appear as a blank value within the column.

Any help greatly appreciated! I've attempted to negate this issue through some IF checks on my Controller as well as some code on the View but haven't yet found a solution.

EDIT:

Chris' solution worked! Below is my updated code segments:

foreach (ApplicationUser user in model)
        {
            if (user.MemberOrgId != null)
            {
                user.Organization = db.MemberOrganizations.Where(o => o.Id == user.MemberOrgId).FirstOrDefault();
            }
            if (user.SponsorOrgId != null)
            {
                user.Sponsor = db.SponsorOrganizations.Where(o => o.Id == user.SponsorOrgId).FirstOrDefault();
            }
        }
        return View(model);

        columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.MemberOrgId.ToString()).Titled("MemOrgID").Sortable(true).Filterable(true).SetWidth(25);
        columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Organization == null ? "" : o.Organization.Name.ToString()).Titled("MemOrg Name.").Sortable(true).Filterable(true).SetWidth(25);
        columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.SponsorOrgId.ToString()).Titled("SponsorID").Sortable(true).Filterable(true).SetWidth(25);
        columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Sponsor == null ? "" : o.Sponsor.Name.ToString()).Titled("Sponsor Name").Sortable(true).Filterable(true).SetWidth(25);
tereško
  • 58,060
  • 25
  • 98
  • 150
Analytic Lunatic
  • 3,853
  • 22
  • 78
  • 120
  • I've seen that post before Daniel. I thought this may be slightly different as I know why and how I am getting `Null` exception, but am trying to find a way around it. Chris has found the Answer :) – Analytic Lunatic Jun 17 '14 at 15:48

1 Answers1

3

Check for a null Organization before trying to get the name?

columns.Add().Encoded(false).Sanitized(false)
    .RenderValueAs(o => o.Organization == null ? "" : o.Organization.Name.ToString())
    .Titled("MemOrg.").Sortable(true).Filterable(true).SetWidth(25);

If you want to combine them into a single column, modify the above code to return the sponsor name if the organization is null.

Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137
  • Thanks for replying Chris! I've tried something similar before, but when I do `columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Organization ? o.Organization.Name.ToString() : "").Titled("MemOrg.").Sortable(true).Filterable(true).SetWidth(25);` the `o.Organization` after `o =>` is flagged as: "Cannot implicitly convert type 'PROJECT.Models.MemberOrganizations' to 'bool'". – Analytic Lunatic Jun 17 '14 at 15:42
  • Sorry, javascript snuck through. The example has been modified – Chris Marasti-Georg Jun 17 '14 at 15:42
  • That did the trick, thank you so much Chris! That is exactly what I was attempting to do but was having quite the time nailing down the syntax. – Analytic Lunatic Jun 17 '14 at 15:51