我遇到的主要问题是带有Select Tag Helper的表单代码中的Null Reference Exception(详见下文)。但是,我正在寻求关于整体方法的建议,包括在页面上的多个视图。
我已经研究了在单个视图页面上组合多个视图的不同方法。 This one使用if-else选项进行布局,使用RenderPartial进行部分视图使用单独的控制器/操作,this one类似于依赖AJAX进行客户端响应,我不知道在参考实现中是否需要或需要。我还查看了其他建议的搜索结果,这些搜索结果与这两个搜索结果并不相同。
所以,我看了,我已经尝试了以下两种方法,使用我编写的代码来选择和显示来自特定db记录的四个数据类别中的每一个的单页面视图结果。需要将四个数据类别依次添加到整个消息页面,因为此页面用于组合整个CAP消息。然后我将在“查看消息”和“批准消息”页面中再次使用它 - 这是第一个EDXL(紧急数据交换语言)项目(哈利路亚!)的终点线。
这是第一次尝试,使用相同的代码来选择我想要显示整个Message的Alert类别的详细信息的记录。
@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>
}
但是,我在这行上得到了一个Null Reference Exception:asp-for=”SelectedAlertIndex” asp-items=”@Model.Alert_Identifiers”>
。此代码与在Index.cshtml页面上成功检索并显示Alert类别的代码之间的唯一区别是,这个代码引用了asp-action=”Details”
而不是Index。
如果我可以使这个版本工作,那么对于其他数据类别,还有三个几乎相同的代码块。
我尝试的另一种方法是使用EdxlCapMessageViewModelsController在这个“Assemble.cshtml”视图页面上使用自定义布局在单个页面上的四个单独的部分视图中的每一个中使用这些相同的代码块之一。不用说,我仍然有相同的Null Reference Exception,我没有看到如何克服这个问题。对于第二个版本,我使用EdxlCapMessageViewModelsController中的minimal action方法:
public IActionResult Assemble()
{
return View();
}
有人建议显示Details操作的控制器代码:
// 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()
我能够通过将代码更改为AlertPick.cshtml来解决第一个问题:
@model edxl_cap_v1_2.Models.ContentViewModels.AlertViewModel
@{
ViewData["Title"] = "AlertPick";
}
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
@{
<h4>@Model.Alerts.Count Alerts</h4>
<form asp-controller="Alerts" asp-action="PickAlert" 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="PickAlert" value="Pick Alert to Assemble EDXL-Cap Message" />
</form>
}
这将在下拉列表中呈现具有Alert_Identifiers作为选项的页面以供选择,并且一旦选择,当单击提交按钮“选择警报以组装EDXL-CAP消息”时,该页面将查看者带到该警报数据类别的详细信息页面。
这是我想要的行为,但我现在想要的是在视图页面上四次具有相同的行为,该视图页面收集完整EDXL-CAP消息的所有数据类别。但是,当我将AlertPick.cshtml复制为部分视图的_AlertPick.cshtml并将四个部分视图集合收集到Assemble.cshtml时,使用显示四个部分视图的布局页面的其他空白页面我没有得到结果我在找...
当我这样做时,第一个局部视图首先在<h4>@Model.Alerts.Count Alerts</h4>
线上抛出另一个Null Reference Exception,当我注释掉第一行时,还在asp-for="SelectedAlertIndex" asp-items="Model.Alert_Identifiers">
上抛出。即使上面的AlertPick.cshtml与_AlertPick.cshtml相同,它也会这样做。所以总体问题只有1/4解决了,这和我在09-14-2018的答案一样多。任何帮助都会受到欢迎,但第一部分已经解决,除非我自己解决这个问题,否则如何完成剩下的工作可能需要另外一个问题。