抱歉,处理您的请求时发生错误。在 MVC3 模型实体中

发布于 2024-11-01 23:34:21 字数 2220 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-11-08 23:34:21

这里发生的情况是,您已将 null 值传递到 index.cshtml 的模型中。
该代码正在对 null 进行 foreach'ing。

在 Prod 中调试:

  • Prod 服务器上的 Listing 表中有多少行?
  • 您的 LINQ To SQL .dbml 是否准确引用 web.config 中的连接字符串,或者它是否在 .designer.cs 中隐藏了隐藏的连接字符串?
  • 尝试在 INDEX.CSHTML 上设置一些调试信息

    @(Model==null) //无论传递的模型数据是否为空,都会写入页面。
    

在开发/测试中对此进行调试:

  • 为了帮助调试它为何为空,请在 Index()。将其设置为return View(listings.ToList());。它实际上是空的吗?然后在视图中检查 null 时设置断点。

foreach 之前,运行 null 检查:

@if (Model != null)
{
     foreach (var foo in Model)
     {
       //your code
     }
}
else
{
    @: Model Is 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:

  • How many rows are in the Listing table on the Prod server?
  • Does your LINQ To SQL .dbml refer exactly to the connection string in your web.config, or does it have a hidden connection string buried within the .designer.cs?
  • Try setting up some debug info on INDEX.CSHTML

    @(Model==null) //will write to page whether the model data being passed is null.
    

Debugging this in Dev/Test:

  • To help debug why it's null, set a breakpoint for yourself on the line in Index(). Set it on return 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:

@if (Model != null)
{
     foreach (var foo in Model)
     {
       //your code
     }
}
else
{
    @: Model Is null!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文