I'm new to coding and started exploring identity having a hard time.
I am trying to get data in 5 tables in 5 dropdowns in Register.cshtml
which is a default page to register a new user.
I am unable to figure out the solution because I and getting Null reference in OnGet
method.
I have changed OnGet
method to:
public async Task<IActionResult> OnGetAsync(string returnUrl = null)
{
ReturnUrl = returnUrl;
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
await LoadInputModel();
return Page();
}
private Task LoadInputModel()
{
InputModel inputModel = new()
{
CompanyList =_unitOfWork.Company.GetAll().Select(i => new SelectListItem()
{
Text= i.Name,
Value = i.Id.ToString()
}),
DepartmentList =_unitOfWork.Department.GetAll().Select(i => new SelectListItem()
{
Text= i.Name,
Value = i.Id.ToString()
}),
DesignationList =_unitOfWork.Designation.GetAll().Select(i => new SelectListItem()
{
Text= i.Name,
Value = i.Id.ToString()
}),
RankList = _unitOfWork.Rank.GetAll().Select(i => new SelectListItem()
{
Text= i.Name,
Value = i.Id.ToString()
}),
CategoryList = _unitOfWork.Category.GetAll().Select(i => new SelectListItem()
{
Text= i.Name,
Value = i.Id.ToString()
})
};
return Task.CompletedTask;
}
I am not sure how to do it... and haven't found anything relevant right so far. I need your help.
By default the OnGet
method is like as follows:
public async Task OnGetAsync(string returnUrl = null)
{
ReturnUrl = returnUrl;
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
}
<div class="row">
<div class="mb-3 col-md-3">
<label class="form-label" asp-for="Input.CategoryId">User type</label>
<select class="form-control" asp-for="Input.CategoryId" asp-items="@Model.Input.CategoryList" id="category">
<option disabled selected>--Select user type--</option>
</select>
</div>
<div class="mb-3 col-md-3">
<label class="form-label" asp-for="Input.DepartmentId">Department</label>
<select class="form-control" asp-for="Input.DepartmentId" asp-items="@Model.Input.DepartmentList" id="department">
<option disabled selected>--Select department--</option>
</select>
</div>
<div class="mb-3 col-md-3">
<label class="form-label" asp-for="Input.DesignationId">Designation</label>
<select class="form-control" asp-for="Input.DesignationId" asp-items="@Model.Input.DesignationList" id="designation">
<option disabled selected>--Select designation--</option>
</select>
</div>
<div class="mb-3 col-md-3">
<label class="form-label" asp-for="Input.RankId">Rank</label>
<select class="form-control" asp-for="Input.RankId" asp-items="@Model.Input.RankList" id="rank">
<option disabled selected>--Select rank--</option>
</select>
</div>
</div>
public class InputModel
{
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
///
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public string? Name { get; set; }
public string? ProfileImage { get; set; }
public string? StreetAddress { get; set; }
public string? City { get; set; }
public string? State { get; set; }
public string? PhoneNumber { get; set; }
public string? PostalCode { get; set; }
public int? CompanyId { get; set; }
[ValidateNever]
public Company Company { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> CompanyList { get; set; }
/// <summary>
/// ////////////////////
/// </summary>
public int? DepartmentId { get; set; }
[ValidateNever]
public Department Department { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> DepartmentList { get; set; }
/// <summary>
///
/// </summary>
public int? DesignationId { get; set; }
[ValidateNever]
public Designation Designation { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> DesignationList { get; set; }
/// <summary>
/// /
/// </summary>
public int? RankId { get; set; }
[ValidateNever]
public Rank rank { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> RankList { get; set; }
/// <summary>
/// /
/// </summary>
public int? CategoryId { get; set; }
[ValidateNever]
public Category category { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> CategoryList { get; set; }
}
Because you are not passing the value of inputModel
to Input
property, so the value of Input
property is null.
Please add Input = inputModel;
in LoadInputModel
method to pass the value. Also in the normalization statement your LoadInputModel
method should end up like this:
private Task LoadInputModel()
{
InputModel inputModel = new InputModel()
{
CompanyList =_unitOfWork.Company.GetAll().Select(i => new SelectListItem()
{
Text= i.Name,
Value = i.Id.ToString()
}),
DepartmentList =_unitOfWork.Department.GetAll().Select(i => new SelectListItem()
{
Text= i.Name,
Value = i.Id.ToString()
}),
//other properties
};
Input = inputModel;
return Task.CompletedTask;
}
Test Result: