I am getting NullReferenceException when I am redirecting to the view from controller. I tried to debug, but debugger stops when it enters in the View and throws an exception.
Here is my code in View.
@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>
My code in Controller, which redirects to this view.
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);
}
I am testing this code by passing productID=1 as a parameter in URL.
here is my database snapshot showing that i have an entry with ID = 1.
Here are few things: 1. I am passing an object to the view from controller. 2. If I comment out ViewBag.Title line, then also i get the same exception 3. I observed through debugging that product object in controller is not null. 4. I tried to add some data to the ViewBag in action, but still same result.
This code was working fine for days, and suddenly started getting this exception. Don't know whats the issue.. I searched everywhere I am not getting the right answer.