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:
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?
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!