I am trying to retrieve data from the database and write the records out, but i am getting an error:
An exception of type 'System.NullReferenceException' occurred in App_Web_uaarpxja.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Database model PastaModel.cs
public class Item
{
[Key]
public int Itemid { get; set; }
public string Itemname { get; set; }
public int Itemprice { get; set; }
public int Currentstock { get; set; }
public string Itemtype { get; set; }
public string pictureURL { get; set; }
}
public class PastaContext : DbContext
{
public PastaContext()
: base("name=PastaModel")
{
Database.CreateIfNotExists();
}
public DbSet<Item> Items { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Controller ItemController.cs
public class ItemController : Controller
{
private PastaContext db = new PastaContext();
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index()
{
return View();
}
}
View Index.cshtml
@model IEnumerable<Oblig1.Models.Item>
<!DOCTYPE html>
<br />
<br />
<html>
<head>
<title>Index</title>
</head>
<body>
@foreach (var item in Model)
{
<p>@item.Itemname</p>
}
</body>
</html>
I am getting this error in this line @foreach (var item in Model) {
Edit: Added DbContext