title: ASP.NET Core MVC with Razor Pages (can't return object from a different model)
body: <p>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 <code>TenantEntityModel</code>. I'd like to simply display those results in my view.</p>
  
  <p>Is it possible to do this and if so, what needs to be in my Razor page model?</p>
  
  <p>The method in my Razor model has the following call:</p>
  
  <pre><code>TenantEntityModel Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);
  </code></pre>
  
  <p>I thought I could just use <code>@Model.Result.DomainName</code> in the view however there is no reference to this object as <code>Result</code> didn't exist in my Razor model. So I tried adding it to my Razor model as:</p>
  
  <pre><code>public TenantEntityModel Result { get; }
  </code></pre>
  
  <p>This allows me to access the properties of the <code>Result</code> in my Razor view. If I put a break point before the <code>return Page()</code> statement I can see the <code>Result</code> object has the properties I'm after. However when the view is rendered, the <code>Model</code> object's <code>Result</code> property is null and I get:</p>
  
  <blockquote>
    <p>NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>I've also tried adding <code>BindProperty</code> as follows:</p>
  
  <pre><code>[BindProperty]
  public TenantEntityModel Result { get; }
  </code></pre>
  
  <p>...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. </p>
  
  <p><strong>View code:</strong></p>
  
  <pre><code>@page
  @model ApiKeyModel
  @if (@Model.Result.ApiKey == null)
              {
                  //show the button
                  &lt;button name="CreateApiKey" type="submit" class="btn btn-primary"&gt;Create new API Key&lt;/button&gt;
              }
  </code></pre>
  
  <p><strong>Controller code:</strong></p>
  
  <pre><code>public class ApiKeyModel : PageModel
  {
      private readonly UserManager&lt;UserRegistrationExtension&gt; _userManager;
      private readonly ILogger&lt;PersonalDataModel&gt; _logger;
  
      [BindProperty]
      public TenantEntityModel Result { get; set; }
  
      public ApiKeyModel(
          UserManager&lt;UserRegistrationExtension&gt; userManager,
          ILogger&lt;PersonalDataModel&gt; logger)
      {
          _userManager = userManager;
          _logger = logger;
      }
  
      public async Task&lt;IActionResult&gt; 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();
      }
  </code></pre>