tags:

views:

16

answers:

1

I am following a simple ADO.NET Entity/MVC 2 tutorial wherein my Views are created by right-clicking the action and selecting 'Add View'. The views get created based on my model and all is good. I can view the initial list of items from the DB but when I click Edit or Delete or Details I get 'Object reference not set to an instance of an object'. It acts like my data is not there at all so I'm thinking I may need to fill ViewData again?

Here is how I am getting the data:

CheckingEntities chk = new CheckingEntities();
        //
        // GET: /CheckingMVC/

        [Authorize]
        public ActionResult Index()
        {
            ViewData.Model = chk.tblCheckings.ToList();
            return View();
        }

And here is an example where I am getting the details:

// GET: /CheckingMVC/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            return View();
        }

I suspect I have filled the ViewData incorrectly or need to do it again but don't know where or how to do that. Still quite new to MVC.

A: 

The values passed to your Views must be populated on every request. Furthermore, values set inside your controller during a request cannot and will not be persisted between requests as every new requests creates a brand new set of controller instances from the controller factory.

In your Details() action you are accepting an id and then returning your View without any data being placed in the Model. Instead try something along these lines:

[Authorize]
public ActionResult Details(int id)
{
   var item = Entities.SomeEntitySet.SingleOrDefault(e => e.Id == id);
   return View("Details", item);
}
Nathan Taylor