title: Problems combining AspNetCore MVC Views together on one view page
body: <p>The main problem I'm having is a Null Reference Exception (detailed below) in form code with a Select Tag Helper. However, I'm seeking advice for the overall approach dor including multiple views together on a page, too.</p>
  
  <p>I’ve looked at different ways to combine multiple views on a single view page. <a href="https://stackoverflow.com/questions/10727795/asp-mvc-combining-views">This one</a> uses if-else choices for layout and RenderPartial for Partial Views with separate controllers/actions and <a href="https://stackoverflow.com/questions/10727795/asp-mvc-combining-views">this one</a> is similar with dependency on AJAX for clientside responsiveness that I don’t know if I want or need in a reference implementation. I’ve also looked at other suggested search results that didn’t come as close as these two.</p>
  
  <p>So, I’ve looked and I’ve tried the two following approaches using the code I worked out for selecting and displaying single page view results for each of four data categories from a specific db record. The four data categories need to added together in turn to the overall message page because this page is for assembling the overall CAP message. I will then use it again in the “Review Message” and the “Approve Message” pages—which is the finish line for this first EDXL (Emergency Data Exchange Language) project (Hallelujah!)</p>
  
  <p>Here is first try, using the same code used to select the record which I want to display the details of the Alert category of the overall Message.</p>
  
  <pre><code>@model edxl_cap_v1_2.Models.ContentViewModels.EdxlCapMessageViewModel
  . . .
  @{
  &lt;h4&gt;Alert&lt;/h4&gt;
  
  &lt;div class="select_container"&gt;
      &lt;form asp-controller="Alerts" asp-action="Details" method="post"&gt;
          &lt;select class="cap_select" id="cap_select" style="width:100%;max-width:95%;"
          asp-for="SelectedAlertIndex" asp-items="@Model.Alert_Identifiers"&gt;
              &lt;option&gt;Select one&lt;/option&gt;
          &lt;/select&gt;
          &lt;br /&gt;
          &lt;input type="submit" name="Details" value="LoadAlert" /&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  }
  </code></pre>
  
  <p>However, I’m getting a Null Reference Exception on the line: <code>asp-for=”SelectedAlertIndex” asp-items=”@Model.Alert_Identifiers”&gt;</code>. The only difference between this code and the code that successfully retrieves and displays the Alert category in a table on the Index.cshtml page is that this one references the <code>asp-action=”Details”</code> rather than Index. </p>
  
  <p>If I can get this version to work, there are three more virtually identical code blocks for the other data categories.</p>
  
  <p>The other approach I tried uses one of these same code blocks in each of  four separate partial views on single page using a custom layout for this “Assemble.cshtml” view page using the EdxlCapMessageViewModelsController. Needless to say, I still have the same Null Reference Exception, and I’m not seeing how to overcome this. For this second version I use the minimal action method in the EdxlCapMessageViewModelsController:</p>
  
  <pre><code>public IActionResult Assemble()
      {
          return View();
      }
  </code></pre>
  
  <p>It was suggested that show the controller code for the Details action:</p>
  
  <pre><code>// GET: Alerts/Details/5
      public async Task&lt;IActionResult&gt; Details(int? id)
      {
          if (id == null)
          {
              return NotFound();
          }
  
          var alert = await _context.Alert
              //.Include(e =&gt; e.Elements)
              //    .ThenInclude(d=&gt; d.DataCategory)
              .AsNoTracking()
              .SingleOrDefaultAsync(m =&gt; m.AlertIndex == id);
  
          if (alert == null)
          {
              return NotFound();
          }
  
          return View(alert);
      }
  </code></pre>
  
  <p>But I also would really appreciate some feedback on the best way approach this problem. I'm adding <code>EdxlCapMessageViewModel.cs</code></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Linq;
  using System.Threading.Tasks;
  using edxl_cap_v1_2.Models;
  using Microsoft.AspNetCore.Mvc.Rendering;
  
  namespace edxl_cap_v1_2.Models.ContentViewModels
  {
      public class EdxlCapMessageViewModel
  {
      [Key]
      public int AlertIndex { get; set; }
      public string Alert_Identifier { get; set; }
      public int SelectedAlertIndex { get; set; }
      [NotMapped]
      public List&lt;SelectListItem&gt; Alert_Identifiers { get; set; }
      public List&lt;AlertVm&gt; Alerts { get; set; }
  
      public Alert Alert { get; set; }
  
      public Info Info { get; set; }
  
      public Area Area { get; set; }
  
      public Resource Resource { get; set; }
  }
  
  }
  </code></pre>
  
  <p>This is the stack trace from the Browser:</p>
  
  <pre><code>NullReferenceException: Object reference not set to an instance of an object.
  AspNetCore._Views_EdxlCapMessageViewModels_Assemble_cshtml+&lt;&lt;ExecuteAsync&gt;b__20_1&gt;d.MoveNext() in Assemble.cshtml
  + 19.    asp-for="SelectedAlertIndex" asp-items="Model.Alert_Identifiers"&gt;
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext+&lt;GetChildContentAsync&gt;d__31.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  System.Runtime.CompilerServices.TaskAwaiter.GetResult()
  Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper+&lt;ProcessAsync&gt;d__7.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner+&lt;RunAsync&gt;d__0.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  AspNetCore._Views_EdxlCapMessageViewModels_Assemble_cshtml+&lt;ExecuteAsync&gt;d__20.MoveNext() in Assemble.cshtml
  + 4    Layout = null;
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.Razor.RazorView+&lt;RenderPageCoreAsync&gt;d__16.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.Razor.RazorView+&lt;RenderPageAsync&gt;d__15.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  System.Runtime.CompilerServices.TaskAwaiter.GetResult()
  Microsoft.AspNetCore.Mvc.Razor.RazorView+&lt;RenderAsync&gt;d__14.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+&lt;ExecuteAsync&gt;d__22.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+&lt;ExecuteAsync&gt;d__21.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.ViewResult+&lt;ExecuteResultAsync&gt;d__26.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+ 
  &lt;InvokeResultAsync&gt;d__19.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+&lt;InvokeNextResultFilterAsync&gt;d__24.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecuted-Context context)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+&lt;InvokeNextResourceFilter&gt;d__22.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(Resource-ExecutedContext context)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+&lt;InvokeFilterPipelineAsync&gt;d__17.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+&lt;InvokeAsync&gt;d__15.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Builder.RouterMiddleware+&lt;Invoke&gt;d__4.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+&lt;Invoke&gt;d__6.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPoint-Middleware+&lt;Invoke&gt;d__4.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPage-Middleware+&lt;Invoke&gt;d__6.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPage-Middleware+&lt;Invoke&gt;d__6.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
  Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+&lt;Invoke&gt;d__7.MoveNext()
  </code></pre>