0

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.

    0

    2 Answers 2

    5

    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.

    Sign up to request clarification or add additional context in comments.

    5 Comments

    That was fast! Thanks! But now another error in the same line appeared... An exception of type 'System.ObjectDisposedException' occurred in EntityFramework.dll but was not handled in user code Additional information: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
    Can you please try to put return View(model); within the scope of using?
    You need to materialize your query, for example .ToList() before you pass it to the view
    Oh yes, Stephen is quite right. Just writing linq prepares everything about the query but does not actually run and fetch data unless you intent to. You can use ToList(), ToArray() etc as Stephen suggested.
    Thanks guys, have a blessed day!
    0

    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);
        }
    
    }
    

    Comments

    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.