简体   繁体   English

ASP.Net MVC项目中的HttpCompileException

[英]HttpCompileException in ASP.Net MVC project

I'm working on an ASP.Net MVC E-commerce project. 我正在开发一个ASP.Net MVC电子商务项目。 I am receiving the following exception. 我收到以下异常。

在此输入图像描述

What is the reason for this exception? 这个例外的原因是什么?

Partial code: 部分代码:

在此输入图像描述

[Third Image here][3] [第三张图片] [3]

a foreach loop needs a definition of some kind of collection ( List , IEnumerable , etc). foreach循环需要某种集合的定义( ListIEnumerable等)。

So in order to fix the issue with your partial view.. at the top you need 所以为了解决你的局部视图的问题..在你需要的顶部

@model IEnumerable<Ecommerce.Models.Products>

then in your main view add 然后在主视图中添加

@Html.Partial("_ProductsView", Model)

so then your foreach is reading.. 'Foreach item in the List of Products' .... perform this operation 那么你的foreach正在阅读..'产品清单中的Foreach项'....执行此操作

With reference of your image its clearing visible that you have forgotten to pass modal to partial view and since the model value is null and if you try iterate so it will throw null object exception. 通过引用您的图像,清除可见,您忘记将模态传递给局部视图,因为模型值为null,如果您尝试迭代,则会抛出null对象异常。 To fix it use following approach. 要修复它,请使用以下方法。

Replace @Html.Partial("_ProductsView") to @Html.Partial("_ProductsView", Model) in your view and again make sure you are passing model from controller. @Html.Partial("_ProductsView") to @Html.Partial("_ProductsView", Model)替换@Html.Partial("_ProductsView") to @Html.Partial("_ProductsView", Model) ,并再次确保从控制器传递模型。

One more thing before iterating list do a null check 迭代列表之前还有一件事是进行空检查

@if(modal != null)
{
   //do your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM