views:

1921

answers:

4

I use Entities Framework to implement my data accsee layer in ASP.NET MVC. I got a problem in View.

When my code in the VIEW something like:

<%= Model.FirstName %>

I got an error in run time:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 14:

Line 15: <%= Model.FirstName %> Line 16:

Line 17:

This is generated from strong-typed view template. I ensured that I added model referrence to web.config I didn't encounter this error when I was using LINQ to SQL

Any help?

A: 

It looks as though your Model is not populated properly in the controller. There's not really enough information in your question to work out what exactly is going wrong though.

Steve Willcock
That's what I found strange. I cannot give more info, becasue I can't. I can use <%=Html.Encode(Model.FirstName) %> but not <% =Model.FirstName %>
Smallville
I'm not sure why that would be the case - could you post your controller code where you populate the Model?
Steve Willcock
+1  A: 

Since i can't comment on an answer i'll do it via a new answer.. I notice that your attribute is called "FirstName", are you by any chance trying to do this tutorial? And is this the Edit action you're having problems with?

Because this is the exact same problem me and 2 of my classmates are having. Here is a more detailed explanation of the problem:

This is the controller action:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Contact contactToEdit) {
    if (!ModelState.IsValid)
        return View();

    try {
        var originalContact = (from c in base._entities.ContactSet
                               where c.Id == contactToEdit.Id
                               select c).First();
        base._entities.ApplyPropertyChanges(originalContact.EntityKey.EntitySetName, contactToEdit);
        base._entities.SaveChanges();

        return RedirectToAction("Index");
    } catch(Exception e) {
        return View();
    }
}

When the ApplyPropertyChanges is called an exception is raised. (InvalidOperationException) Exception message:

{System.InvalidOperationException: The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'ContactManager.Models.Contact'.
at System.Data.Objects.ObjectContext.ApplyPropertyChanges(String entitySetName, Object changed)
at ContactManager.Controllers.HomeController.Edit(Contact contactToEdit) in C:\Users\Jeroen\Desktop\ContactManager\ContactManager\ContactManager\Controllers\HomeController.cs:line 65}

When I add this line before ApplyPropertyChanges:

System.Data.Objects.ObjectStateEntry x = base._entities.ObjectStateManager.GetObjectStateEntry(originalContact);

x does contain an entry of the correct type. (debug image)

Note: I made a small deviation from the tutorial and put the entities object in a superclass so I don't have to declare the same object in all my controllers. But the same problem arises when following the tutorial.

Jeroen Pelgrims
A: 

I figured it out! The problem was in the VIEW Normally, it would be, using (Html.BeginForm(new { Id = Model.Id }))

If the primary key in your database was CategoryID, you would think you should adapt the code to

Id = Model.CategoryID

BUT, actually, you should do it like this,

using (Html.BeginForm(new { CategoryID = Model.CategoryID }))

Otherwise, it won't populate the model.

Resurge, hope it helps!

Smallville
A: 

Sometimes Model is null because the select statement with the method .FirstOrDefault() did not bring a valid object.

You can always change .FirstOrDefault() to .First() and recompile your solution.

Junior Mayhé