C# 从控制器重定向时视图上出现MVC 4 NullReferenceException

C# 从控制器重定向时视图上出现MVC 4 NullReferenceException,c#,asp.net-mvc,asp.net-mvc-4,razor,nullreferenceexception,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,Nullreferenceexception,当我从控制器重定向到视图时,收到NullReferenceException。我试图调试,但调试器进入视图并引发异常时停止 这是我的代码 @model MvcApplication2.Models.Product @{ ViewBag.Title = "ProductDetails"; //NullReferenceException Here. If I comment this line, then page doesn't even load, and I get "No Sou

当我从控制器重定向到视图时,收到NullReferenceException。我试图调试,但调试器进入视图并引发异常时停止

这是我的代码

@model MvcApplication2.Models.Product

@{
    ViewBag.Title = "ProductDetails"; //NullReferenceException Here. If I comment this line, then page doesn't even load, and I get "No Souce found".
}

<div class="span9">
    <ul class="breadcrumb">
    <li><a href=@Url.Action("Index", "Home") >Home</a> <span class="divider">/</span></li>
    <!--<li><a href= >Products</a> <span class="divider">/</span></li>-->
    <li class="active">product Details</li>
    </ul>
</div>
我通过在URL中传递productID=1作为参数来测试这段代码

这是我的数据库快照,显示我有一个ID为1的条目

这里有几件事: 1.我正在从控制器向视图传递一个对象。 2.如果我注释掉ViewBag.Title行,那么我也会得到相同的异常 3.我通过调试观察到控制器中的product对象不是null。 4.我试图在ViewBag中添加一些数据,但结果仍然相同


这段代码连续几天运行良好,突然出现了这个异常。不知道是什么问题。。我搜索了所有没有得到正确答案的地方。

注释表明在ViewBag行之外发生了其他事情。您是否尝试过
.FirstOrDefault()
扩展方法?ViewBag不会导致问题几乎所有的
NullReferenceException
情况都是相同的。有关一些提示,请参见“”。@JohnSaunders-这更多的是一个“调试nullref失败”的问题。涉及剃须刀,所以不是一般的欺骗。@HenkHolterman:这个场景的简化版本可能是对链接问题的一个很好的补充。
 public ActionResult ProductDetails(int productID=0)
        {
            //read product from the database
            Product product = (from item in shoppingDb.products where item.ID ==     productID select item).SingleOrDefault();
            Session["CurrentUrl"] = "/Products/ProductDetails?productID=" + productID;
            //Session["ReturnProductsUrl"] = Request.Path.ToString();
            //ViewBag.Comment = "this is test";
            return View(product);
        }