C
C#•2w ago
AXED

I'm having a weird null reference issue with a Razor Pages view component.

I have a shared layout where I'm trying to pass either a student or teacher ID (or 0 if none) to a view component. Even though I'm explicitly handling null cases I keep getting a NullReferenceException. Here's my layout code:
@if (!ViewContext.RouteData.Values["page"].ToString().Contains("/Account"))
{
// Retrieve studentId and teacherId from ViewData
long? studentId = ViewData["StudentId"] as long?;
long? teacherId = ViewData["TeacherId"] as long?;

// Assign actualId to 0 if both are null
long actualId = studentId ?? teacherId ?? 0;

// Initialize userType as null
string? userType = null;
if (actualId != 0)
{
userType = studentId.HasValue ? "Student" : "Teacher";
}

@await Component.InvokeAsync("UserHeaderInfo", new { id = actualId, type = userType })
}
@if (!ViewContext.RouteData.Values["page"].ToString().Contains("/Account"))
{
// Retrieve studentId and teacherId from ViewData
long? studentId = ViewData["StudentId"] as long?;
long? teacherId = ViewData["TeacherId"] as long?;

// Assign actualId to 0 if both are null
long actualId = studentId ?? teacherId ?? 0;

// Initialize userType as null
string? userType = null;
if (actualId != 0)
{
userType = studentId.HasValue ? "Student" : "Teacher";
}

@await Component.InvokeAsync("UserHeaderInfo", new { id = actualId, type = userType })
}
view component:
public class UserHeaderInfoViewComponent : ViewComponent
{
private readonly IUserService _userService;
public UserHeaderInfoViewComponent(IUserService userService)
{
_userService = userService;
}

public async Task<IViewComponentResult> InvokeAsync(long id, string? type = null)
{
UserHeaderDto user = null;
if (id == 0)
{
user = _userService.GetUserHeaderInfo();
}
else if (type == "Student")
{
user = _userService.GetStudentHeaderInfo(userId: null, studentId: id);
}
else if (type == "Teacher")
{
user = _userService.GetTeacherHeaderInfo(userId: null, teacherId: id);
}

if (user == null)
{
user = new UserHeaderDto();
}
return View("Default", user);
}
}
public class UserHeaderInfoViewComponent : ViewComponent
{
private readonly IUserService _userService;
public UserHeaderInfoViewComponent(IUserService userService)
{
_userService = userService;
}

public async Task<IViewComponentResult> InvokeAsync(long id, string? type = null)
{
UserHeaderDto user = null;
if (id == 0)
{
user = _userService.GetUserHeaderInfo();
}
else if (type == "Student")
{
user = _userService.GetStudentHeaderInfo(userId: null, studentId: id);
}
else if (type == "Teacher")
{
user = _userService.GetTeacherHeaderInfo(userId: null, teacherId: id);
}

if (user == null)
{
user = new UserHeaderDto();
}
return View("Default", user);
}
}
I keep getting:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.NullReferenceException: 'Object reference not set to an instance of an object.'
in the layout at the ternary operation in the layout. I've verified the ViewData values are indeed null. Any ideas what I'm missing? 🤔
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server