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);