我遇到的主要问题是带有选择标记帮助器的表单代码中的空引用异常(详细信息如下)。但是,我也在为总体方法寻求建议,包括在页面上同时包含多个视图。
我研究了在单个视图页面上组合多个视图的不同方法。 This one使用if-else选项进行布局,使用RenderPartial选择具有单独的控制器/动作的部分视图,this one与依赖AJAX的客户端响应能力类似,我不知道自己是否需要参考实施。我还查看了其他建议的搜索结果,但搜索结果却不如这两个。
因此,我查看了我已经尝试过的以下两种方法,这些方法是使用我为从特定数据库记录中选择和显示四个数据类别的每个页面的单页浏览结果而设计的代码。这四个数据类别需要依次添加到总体消息页面中,因为此页面用于组合总体CAP消息。然后,我将在“审阅消息”和“批准消息”页面中再次使用它-这是第一个EDXL(紧急数据交换语言)项目(Hallelujah!)的终点线。
这是第一次尝试,使用与选择要显示整个消息的“警报”类别的详细信息的记录相同的代码。
@model edxl_cap_v1_2.Models.ContentViewModels.EdxlCapMessageViewModel
. . .
@{
<h4>Alert</h4>
<div class="select_container">
<form asp-controller="Alerts" asp-action="Details" method="post">
<select class="cap_select" id="cap_select" style="width:100%;max-width:95%;"
asp-for="SelectedAlertIndex" asp-items="@Model.Alert_Identifiers">
<option>Select one</option>
</select>
<br />
<input type="submit" name="Details" value="LoadAlert" />
</form>
</div>
}
但是,我在asp-for=”SelectedAlertIndex” asp-items=”@Model.Alert_Identifiers”>
这行上收到了Null Reference Exception。此代码与成功检索并在Index.cshtml页上的表中显示“警报”类别的代码之间的唯一区别是,此代码引用的是asp-action=”Details”
而不是Index。
如果我可以使用该版本,则其他数据类别还有三个几乎完全相同的代码块。
我尝试的另一种方法是使用EdxlCapMessageViewModelsController的“ Assemble.cshtml”视图页面的自定义布局,在单个页面上的四个单独的局部视图的每个视图中使用这些相同的代码块之一。不用说,我仍然有相同的Null Reference Exception,并且我没有看到如何克服这个问题。对于第二个版本,我在EdxlCapMessageViewModelsController中使用最小操作方法:
public IActionResult Assemble()
{
return View();
}
建议显示“详细信息”操作的控制器代码:
// GET: Alerts/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var alert = await _context.Alert
//.Include(e => e.Elements)
// .ThenInclude(d=> d.DataCategory)
.AsNoTracking()
.SingleOrDefaultAsync(m => m.AlertIndex == id);
if (alert == null)
{
return NotFound();
}
return View(alert);
}
但我也非常感谢您提供有关解决此问题的最佳方法的反馈。我要添加EdxlCapMessageViewModel.cs
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<SelectListItem> Alert_Identifiers { get; set; }
public List<AlertVm> Alerts { get; set; }
public Alert Alert { get; set; }
public Info Info { get; set; }
public Area Area { get; set; }
public Resource Resource { get; set; }
}
}
这是来自浏览器的堆栈跟踪:
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore._Views_EdxlCapMessageViewModels_Assemble_cshtml+<<ExecuteAsync>b__20_1>d.MoveNext() in Assemble.cshtml
+ 19. asp-for="SelectedAlertIndex" asp-items="Model.Alert_Identifiers">
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext+<GetChildContentAsync>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+<ProcessAsync>d__7.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner+<RunAsync>d__0.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
AspNetCore._Views_EdxlCapMessageViewModels_Assemble_cshtml+<ExecuteAsync>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+<RenderPageCoreAsync>d__16.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Mvc.Razor.RazorView+<RenderPageAsync>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+<RenderAsync>d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+<ExecuteAsync>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+<ExecuteAsync>d__21.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Mvc.ViewResult+<ExecuteResultAsync>d__26.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+
<InvokeResultAsync>d__19.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResultFilterAsync>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+<InvokeNextResourceFilter>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+<InvokeFilterPipelineAsync>d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPoint-Middleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPage-Middleware+<Invoke>d__6.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPage-Middleware+<Invoke>d__6.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger-Notification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()