抱歉,处理您的请求时发生错误。在 MVC3 模型实体中
我是 MVC 新手,制作了一个可以在本地计算机(带 SQLExpress 的 Windows 7)上运行的应用程序,但是当我在 Windows 2008 R2、SQL 2008 R2 和 IIS7 上实时部署时,我收到此错误:“抱歉,处理时发生错误您的请求”
下面是相关代码的片段:
WEB.CONFIG connectionStrings
add name="RentalEntities" connectionString="Data Source=[ServerName];Initial Catalog=[DatabaseName];Integrated Security=True" providerName="System.Data.SqlClient"
HOMECONTROLLER
namespace Rental.Controllers
{
RentalEntities rentalDB = new RentalEntities();
...
public ActionResult Index()
{
var listings = from l in rentalDB.Listings select l;
return View(listings.ToList());
}
}
HOME/INDEX.CSHTML
@model List<Rental.Models.Listing>
@foreach (var listing in Model)
{
@listing.ListingName <br />
}
此行发生错误 @foreach (模型中的 var 列表)
,与 null 有关 - 请注意,数据库中没有任何字段为 null。任何帮助将不胜感激。
以下是完整的错误消息:[ 其中第 13 行是:foreach(模型中的变量列表)
Sorry, an error occurred while processing your request.
System.NullReferenceException: Object reference not set to an instance of an object. at ASP._Page_Views_Home_Index_cshtml.Execute() in c:\inetpub\wwwroot\projects\Rentals\Views\Home\Index.cshtml:line 13 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
I am new in MVC, made an app that works on my local machine (Windows 7 with SQLExpress) but when I deploy live on Windows 2008 R2, SQL 2008 R2 and IIS7, I get this error: "Sorry, an error occurred while processing your request"
Below are snippets of the code in question:
WEB.CONFIG connectionStrings
add name="RentalEntities" connectionString="Data Source=[ServerName];Initial Catalog=[DatabaseName];Integrated Security=True" providerName="System.Data.SqlClient"
HOMECONTROLLER
namespace Rental.Controllers
{
RentalEntities rentalDB = new RentalEntities();
...
public ActionResult Index()
{
var listings = from l in rentalDB.Listings select l;
return View(listings.ToList());
}
}
HOME/INDEX.CSHTML
@model List<Rental.Models.Listing>
@foreach (var listing in Model)
{
@listing.ListingName <br />
}
The error occurred on this line @foreach (var listing in Model)
, something to do with being null - note that NONE of the fields in the database are null. Any help will be appreciated.
Below is the full error message: [ where line 13 is: foreach (var listing in Model)
Sorry, an error occurred while processing your request.
System.NullReferenceException: Object reference not set to an instance of an object. at ASP._Page_Views_Home_Index_cshtml.Execute() in c:\inetpub\wwwroot\projects\Rentals\Views\Home\Index.cshtml:line 13 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里发生的情况是,您已将 null 值传递到
index.cshtml
的模型中。该代码正在对
null
进行 foreach'ing。在 Prod 中调试:
尝试在 INDEX.CSHTML 上设置一些调试信息
在开发/测试中对此进行调试:
Index()
。将其设置为return View(listings.ToList());
。它实际上是空的吗?然后在视图中检查 null 时设置断点。在
foreach
之前,运行 null 检查:What has happened here is that you've passed a null value into the Model for
index.cshtml
.The code is foreach'ing over
null
.Debugging this in Prod:
Try setting up some debug info on INDEX.CSHTML
Debugging this in Dev/Test:
Index()
. Set it onreturn View(listings.ToList());
. Is it actually null? Then set a breakpoint on the check for null in your View.Before
foreach
, run a check for null: