Sorry, an error occurred while processing your request. in MVC3 Model Entities [duplicate]
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.Mode开发者_运维知识库ls.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)
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 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:
@if (Model != null)
{
foreach (var foo in Model)
{
//your code
}
}
else
{
@: Model Is null!
}
精彩评论