1
@model IEnumerable<SportsStore.Domain.Entities.Product>.....this is my view List.cshtml

@{
    ViewBag.Title = "Products";
}
@foreach (var j in Model) //getting error at Model
{ 
    <div class="item">
        <h3>@j.Name</h3>
        @j.Description
        <h4>@j.Price.ToString("C")</h4>
    </div>
}

the sportstore example in pro Asp.Net MVC3 book chapter 7 before the part where i can prepare a database

this is my controller

 public class ProductController : Controller
    {
        public ProductController()
        { 
        }
        //
        // GET: /Product/
        private IProductRepository repository;
        public ProductController(IProductRepository productrepository)
        {
            repository=productrepository;
        }
        public ViewResult List()
        {
            return View();
        }
3
  • 1
    Would you share the index code in the controller? Commented Jun 20, 2013 at 17:13
  • I added the error's context to the question title Commented Jun 20, 2013 at 17:40
  • Thank you neontapir but how am i supposed to deal with this pls help i am new to ASP.Net MVC Commented Jun 21, 2013 at 9:07

3 Answers 3

1

when you pass IEnumerable you should pass it as List or something like that, for example your Index Action Method should be like :

public ActionResult Index()
{
  return View(_dbContext.Product.ToList());
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to initialize the Model I suppose, just like we do in C#: Suppose we have a class, then we initialize the same like:

Model m1= new Model();

Make sure you check the null also for all items you are passing.

5 Comments

This is likely the correct explanation. If you use View() to return your view, Model is null. You'd want to use the View(model) overload to provide a model object.
@neontapir @model IEnumerable<SportsStore.Domain.Entities.Product> is Model isn't it?
Yes... but if you don't pass it in the View() call... you end up with a null model.
Correct, @model defines the type of the model, but not an instance of its current value.
Yes the error i am getting is that the model is null ..it is not initialized .. i am quite new to ASP.NetMvc pls help guys
0

In the EFDbContext class i inherited DbConntext and that worked for me, hope it'll help you too:

using SportsStore.Domain.Entities;
using System.Data.Entity;

namespace SportsStore.Domain.Concrete
{
public class EFDbContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
    }
}

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.