I'm making a call to Azure tables in a method I've created as part of the Razor page model (cs) file. The results are of type TenantEntityModel. I'd like to simply display those results in my view.
Is it possible to do this and if so, what needs to be in my Razor page model?
The method in my Razor model has the following call:
TenantEntityModel Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);
I thought I could just use @Model.Result.DomainName in the view however there is no reference to this object as Result didn't exist in my Razor model. So I tried adding it to my Razor model as:
public TenantEntityModel Result { get; }
This allows me to access the properties of the Result in my Razor view. If I put a break point before the return Page() statement I can see the Result object has the properties I'm after. However when the view is rendered, the Model object's Result property is null and I get:
NullReferenceException: Object reference not set to an instance of an object.
I've also tried adding BindProperty as follows:
[BindProperty]
public TenantEntityModel Result { get; }
...but that didn't work either. So I'm at a loss on how to take the results of a query, populate an object with a model from a different class, then render a page with the results of that object.
View code:
@page
@model ApiKeyModel
@if (@Model.Result.ApiKey == null)
{
//show the button
<button name="CreateApiKey" type="submit" class="btn btn-primary">Create new API Key</button>
}
Controller code:
public class ApiKeyModel : PageModel
{
private readonly UserManager<UserRegistrationExtension> _userManager;
private readonly ILogger<PersonalDataModel> _logger;
[BindProperty]
public TenantEntityModel Result { get; set; }
public ApiKeyModel(
UserManager<UserRegistrationExtension> userManager,
ILogger<PersonalDataModel> logger)
{
_userManager = userManager;
_logger = logger;
}
public async Task<IActionResult> OnGet()
{
// need to validate if account exists first
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
var domainName = user.Office365DomainName;
var id = user.Id;
AzureTableConnection azureTableConnection = new AzureTableConnection();
TenantEntityModel Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);
return Page();
}