0

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

1 Answer 1

3

This is happening because you are not injecting anything in your view from controller from where you are returning view.

and your view expecting IEnumerable<Oblig1.Models.Item> from your controller but as you are not passing model from controller the view gets null in Model.

try below code :-

public ActionResult Index()
    {
        var model = db.Items.ToList(); ////Your model that you want to pass
        return View(model);
    }
Sign up to request clarification or add additional context in comments.

9 Comments

Then i getting, The model item passed into the dictionary is of type 'System.Data.Entity.DbSet1[Oblig1.Models.Item]', but this dictionary requires a model item of type 'Oblig1.Models.Item'.` when i try it.
New error... The model item passed into the dictionary is of type 'System.Collections.Generic.List``1[Oblig1.Models.Item]', but this dictionary requires a model item of type 'Oblig1.Models.Item' @Neel
have you made changes in ur view ? @mv700 your view expecting ienumerable
is this first line of ur view "@model IEnumerable<Oblig1.Models.Item>" ? @mv700
Yes, @model IEnumerable<Oblig1.Models.Item> @Neel
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.