I've checked the million other posts for the same error on here but couldn't find anything that helps.
I have a model like this:
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public int? CommunityId { get; set; }
public string Email { get; set; }
public Int64? Phone { get; set; }
public AlertMode AlertMode { get; set; }
public string B64EncodedImage { get; set; }
}
When I get the user from the database, the assignment looks like this:
User user = new User()
{
UserId = Convert.ToInt32(reader["UserId"]),
UserName = Convert.ToString(reader["UserName"]),
CommunityId = reader["CommunityId"] == DBNull.Value
? (int?)null : Convert.ToInt32(reader["CommunityId"]),
Phone = reader["Phone"] == DBNull.Value
? (Int64?)null : Convert.ToInt64(reader["Phone"]),
AlertMode = (AlertMode)Int32.Parse(reader["AlertMode"].ToString()),
Email = Convert.ToString(reader["Email"]),
B64EncodedImage = Convert.ToString(reader["B64EncodedImage"])
};
When I call this code:
@((BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId)).CommunityId.HasValue
? "COMMUNITYID"
: "Not set!"
)
I get this error:
Nullable object must have a value.
On this line:
@((BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId)).CommunityId.HasValue
Any ideas why?
====================================================================== EDIT:
I changed the code to return the User to the view as a model:
public ActionResult Manage()
{
User user = BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId);
return View(user);
}
Stepping through, the model is populated but the CommunityId is null (which should be fine)
@(Model.CommunityId.HasValue
? "COMMUNITYID"
: "Not set!"
)
Now I get:
Cannot perform runtime binding on a null reference