2014-04-17 24 views
0
public class StoreController : Controller 
{ 
    // GET: /Store/ 

    ProductsStoreEntities storeDB = new ProductsStoreEntities(); 
    public ActionResult Index() 
    { 
     var items = storeDB.item.ToList(); 
     return PartialView(items); 
    } 

索引視圖MVC 3回到0

model IEnumerable<MvcProductsStore.Models.Item> 
@{ 
    ViewBag.Title = "Store"; 
} 

<h3>Browse Products:</h3> 


<p> 
    Select from @Model.Count() 
    Products: 
</p> 

<ul> 
    @foreach (var item in Model) 
    { 
     <li>@Html.ActionLink(item.IName,"Browse", new { item = item.IName })</li> 
    } 
</ul> 

樣本數據

namespace MvcProductsStore.Models 
{ 
    public class SampleData : DropCreateDatabaseIfModelChanges<ProductsStoreEntities> 
    { 
     protected override void Seed(ProductsStoreEntities context) 
     { 
      var items = new List<Item> 
      { 
       new Item {ItemId = 1000, IName = "Patches"}, 
       new Item {ItemId = 2000,IName = "Jewelry"}, 
       new Item {ItemId = 3000, IName = "Wood Working"} 
      }; 
      items.ForEach(s => context.item.Add(s)); 
     } 
    } 

模式

namespace MvcProductsStore.Models 
{ 
    public class ProductsStoreEntities : DbContext 
    { 
     public DbSet<Product> products { get; set; } 
     public DbSet<Item> item { get; set; } 
     protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
     } 
    } 
} 
計數

不是真的返回錯誤,但是當我點擊商店時,它告訴我從0項中選擇。因此,而不是拿起它得到的0

回答

1

計數你種方法 後尚未保存的數據庫上下文對象的項目列表中,這樣你應該添加

context.SaveChanges(); 
樣本數據中部分

,種子方法

0

請更改返回View()而不是PartialView()。

public class StoreController : Controller 
{ 
    // GET: /Store/ 

    ProductsStoreEntities storeDB = new ProductsStoreEntities(); 
    public ActionResult Index() 
    { 
     var items = storeDB.item.ToList(); 
     return View(items); 
    } 
+0

添加保存消息並更改返回PartialView返回視圖還有什麼我失蹤仍然給我從0項中選擇。 – user3543447