Caloyski
Caloyski

Reputation: 368

NullReferenceException was unhandled asp.net entity framework

I'm new to asp.net mvc and I'm doing this exercise for myself. I created an edmx from Northwind Database. I created a controller:

 public ActionResult Index(int id)
    {
        var model = new IndexViewModel();
        using (var db = new ProductDB())
        {
            model.Products = from p in db.Products
                             orderby p.ProductName

                             select new IndexViewModel.InfoProduct
                             {
                                 ProductName = p.ProductName,
                                 QuantityPerUnit = p.QuantityPerUnit,
                                 UnitPrice = p.UnitPrice
                             };
        }
        return View();
    }

...the view:

    @model aspTest.Models.IndexViewModel
@{
    Layout = null;
}

...

    <div> <ul>
        @foreach (var p in Model.Products){
    <li>@p.ProductName</li>
}
        </ul>
    </div>

...and the ViewModel:

public class IndexViewModel
{
    public IEnumerable<InfoProduct> Products { get; set; }

    public class InfoProduct
    {            
        public string ProductName { get; set; }
    }

}

But this error keeps on appearing in this part: @foreach (var p in Model.Products){

  • @p.ProductName
  • Sorry, I know this might be noobish to most of you.

    Upvotes: 0

    Views: 340

    Answers (2)

    Marco
    Marco

    Reputation: 23945

    Besides the fact that you return no ViewModel to your view and shout use return View(model); there is something else fishy with your code:

    public class InfoProduct
    {            
        public string ProductName { get; set; }
    }
    

    Your class InfoProduct only contains a Productname, yet you also assign a Quantity and Price property in your select clause, this won't compile.

    model.Products = from p in db.Products
                     orderby p.ProductName
                     select new IndexViewModel.InfoProduct
                     {
                          ProductName = p.ProductName,
                     };
    

    Lastly, materialize your data using .ToList()

    public ActionResult Index(int id)
    {
        var model = new IndexViewModel();
        using (var db = new ProductDB())
        {
           model.Products = (from p in db.Products
                             orderby p.ProductName
                             select new IndexViewModel.InfoProduct
                             {
                                 ProductName = p.ProductName,
                             }).ToList();
           return View(model);
        }
    
    }
    

    Upvotes: 0

    user3021830
    user3021830

    Reputation: 2924

    You are declaring in your model that it will work with the IndexViewModel class as:

    @model aspTest.Models.IndexViewModel
    

    but you are sending nothing to the View from Controller as :

    return View();
    

    Try using:

    return View(model);
    

    The error is quite true, you are trying to iterate on properties (Products) of an object (IndexViewModel) which you don't provide.

    Upvotes: 5

    Related Questions