【发布时间】:2020-05-15 22:31:40
【问题描述】:
我有一个像这样循环的视图
@foreach (var item in Model.RoutineAttachments)
{
<tr>
<td>@item.Attachment.Name</td>
<td>@item.Attachment.Weight</td>
<td>@item.Attachment.Thickness</td>
<td>@item.IsGeneric</td>
<td>@Html.ActionLink("Delete", "DeleteAttachment", new { routineId = item.RoutineId, attachmentId = item.Attachment.Id }, new { @class = "btn btn-danger btn-sm" })</td>
</tr>
}
当我访问该页面时,我得到一个 InvalidOperationException,其中令人烦恼的部分是“序列不包含任何元素”。
完整的堆栈跟踪:
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.First(IEnumerable`1 source) +335
ASP._Page_Views_Routine_EditAttachments_cshtml.Execute() in c:\Project\Dashboard\Views\EditAttachments.cshtml:19
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
System.Web.WebPages.StartPage.RunPage() +17
System.Web.WebPages.StartPage.ExecutePageHierarchy() +73
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +198
System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar) +161
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +128
我在从控制器返回和进入循环时检查了集合不为空,以确保这不是问题。但是一旦它试图获取循环中的第一项,我就会得到异常。这不应该发生,因为它是一个 foreach 循环。
但是,在视图的更下方,我有这部分:
<input type="hidden" value=@Model.RoutineAttachments.First().RoutineId />
改成:
<input type="hidden" value=@Model.RoutineAttachments.FirstOrDefault()?.RoutineId />
解决了根本问题。
然而,我的问题是,为什么在访问 foreach 循环时抛出异常,然后显示不正确的堆栈跟踪,而不是在访问不存在的项目时抛出?
编辑: 由于人们回答了错误的问题,因此添加了更多证据。请尝试回答实际问题。
编辑 2:
我创建了一个最小的可复制版本
控制器:
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
TestModel t = new TestModel();
t.TestList = new List<TestItem>();
return View(t);
}
}
public class TestModel
{
public List<TestItem> TestList { get; set; }
}
public class TestItem
{
public string S { get; set; }
}
查看:
@model Dashboard.Controllers.TestModel
@foreach (var item in Model.TestList)
{
<h5>@item.S</h5>
}
<input type="hidden" value="@Model.TestList.First().S" />
似乎由于某种原因,它在 foreach 循环之前用 First() 解析了该行,但在访问 foreach 循环之前不会抛出异常?
【问题讨论】:
-
向我们展示您的控制器的操作。这样就更容易确定实际问题是什么。
-
在Model构造函数中初始化RoutineAttachments是否有效?类似
RoutineAttachments = new List<Attachment>()。视图顶部是否正确定义了模型类型? -
另外请展示您的模型和整个视图
-
是的,模型在视图顶部正确定义。并且该列表已初始化,因为它不为空且仅为空。
-
视图也几乎只是我已经展示的内容。还有一些 div 等等。
标签: c# asp.net linq asp.net-mvc-5