0

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.

DatabaseSnapshot

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.

Glorfindel
  • 19,729
  • 13
  • 67
  • 91
Amit
  • 371
  • 2
  • 5
  • 17
  • The comment suggest something else happens w/o the ViewBag line. – Henk Holterman May 03 '14 at 19:36
  • 1
    Have you tried the `.FirstOrDefault()` extension method? The ViewBag can't cause a problem –  May 03 '14 at 20:20
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 03 '14 at 21:13
  • @JohnSaunders - this is more of a "failure to debug a nullref" question. Involving Razor, so not a run of the mill dupe. – Henk Holterman May 03 '14 at 21:31
  • @HenkHolterman: a simplified version of this scenario might be a good addition to the linked question. – John Saunders May 03 '14 at 21:32
  • Look into your Layout page, I suspect the nullref might be there. Layout is often defined in _viewstart.cshtml file – Yishai Galatzer May 03 '14 at 23:51
  • Please paste all of the code for your view. Are you not perhaps referencing related entities for product in your view, which have not been loaded? –  May 22 '14 at 21:10

0 Answers0