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

データベースを準備できる部分の前に、pro Asp.Net MVC3本の第7章のsportstoreの例

これは私のコントローラーです

 public class ProductController : Controller
    {
        public ProductController()
        { 
        }
        //
        // GET: /Product/
        private IProductRepository repository;
        public ProductController(IProductRepository productrepository)
        {
            repository=productrepository;
        }
        public ViewResult List()
        {
            return View();
        }
4

3 に答える 3

1

渡すIEnumerableときは、リストなどとして渡す必要があります。たとえば、インデックスアクションメソッドは次のようにする必要があります。

public ActionResult Index()
{
  return View(_dbContext.Product.ToList());
}
于 2013-06-20T18:05:47.520 に答える
0

C# で行うのと同じように、Model を初期化する必要があります。クラスがあるとします。次に、同じように初期化します。

Model m1= new Model();

渡すすべてのアイテムについても null を確認してください。

于 2013-06-20T17:01:41.800 に答える
0

EFDbContext クラスで私は DbConntext を継承しましたが、それは私にとってはうまくいきました。あなたにも役立つことを願っています:

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

namespace SportsStore.Domain.Concrete
{
public class EFDbContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
    }
}
于 2013-07-24T12:17:49.500 に答える