Asp.net beginner issues

时间:2015-10-02 15:46:07

标签: c# asp.net-mvc entity-framework

So I'm attempting to write a practice asp.net website for a bike store network. If you look at my index view for my store object, As well as my mock db and controller, you can see that I intend for the view to print out the number and the names of the stores:

BikeStoreEntities.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using BikeStore.Models;

namespace BikeStore.Models
{
    public class BikeStoreEntities : DbContext
    {
        public DbSet<Inventory> StoreInventory { get; set; }
        public DbSet<Store> Stores { get; set; }
    }
}

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BikeStore.Models;


namespace BikeStore.Controllers
{
    public class StoreController : Controller
    {
        BikeStoreEntities storeDB = new BikeStoreEntities();
        // GET: Store
        public ActionResult Index()
        {
            var stores = storeDB.Stores.ToList();
            return View(stores);  
        }
        public ActionResult Details(int id)
        {
            var bike = new Inventory {SerialNumber=id };
            return View(bike);
        }
        public ActionResult Browse(string name)
        {
            var store = new Store { Name = name };
            return View(store);
        }
    }
}

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using BikeStore.Models;
using BikeStore.Models;

namespace BikeStore.Models
{
    public class SampleData : DropCreateDatabaseIfModelChanges<BikeStoreEntities>
    {
        protected override void Seed(BikeStoreEntities context)
        {
            var stores = new List<Store>
            {
                new Store { Name = "Rocky Road" , City= " ", Address= " ", Phone= " ", Employees = new List<Employee>(), StoreInventory= new List<Inventory>()},
                new Store { Name = "Jazzy Drive" ,City= " ", Address= " ", Phone= " ", Employees = null, StoreInventory= null},
                new Store { Name = "Metal Foundry" ,City= " ", Address= " ", Phone= " ", Employees = null, StoreInventory= null}
            };
        }
    }
}

View

 @model IEnumerable<BikeStore.Models.Store>

@{
    ViewBag.Title = "Store";
}
<h3>Browse Stores</h3>
<p>
    Select from @Model.Count() Stores:
</p>
<ul>
    @foreach (var store in Model)
    {
        <li>@store.Name</li>
    }
</ul>

However, It prints out "Select from 0 Stores" and then no stores after it. Does anyone have any idea what might be going on here? I would be happy to provide any other project files that you feel are relevant.

1 个答案:

答案 0 :(得分:2)

您没有填充实体,您的本地变量超出范围,也没有添加任何数据。

namespace BikeStore.Models
{
    public class SampleData :    DropCreateDatabaseIfModelChanges<BikeStoreEntities>
    {
        protected override void Seed(BikeStoreEntities context)
        {
            var stores = new List<Store>
            {
                new Store { Name = "Rocky Road" , City= " ", Address= " ", Phone= " ", Employees = new List<Employee>(), StoreInventory= new List<Inventory>()},
                new Store { Name = "Jazzy Drive" ,City= " ", Address= " ", Phone= " ", Employees = null, StoreInventory= null},
                new Store { Name = "Metal Foundry" ,City= " ", Address= " ", Phone= " ", Employees = null, StoreInventory= null}
            };
        }
    }
}

请参阅此处有关如何播种的示例:http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-3

protected override void Seed(BookService.Models.BookServiceContext context)
{
    context.Authors.AddOrUpdate(x => x.Id,
    new Author() { Id = 1, Name = "Jane Austen" },
    new Author() { Id = 2, Name = "Charles Dickens" },
    new Author() { Id = 3, Name = "Miguel de Cervantes" }
    );

    context.Books.AddOrUpdate(x => x.Id,
    new Book() { Id = 1, Title = "Pride and Prejudice", Year = 1813, AuthorId = 1, 
        Price = 9.99M, Genre = "Comedy of manners" },
    new Book() { Id = 2, Title = "Northanger Abbey", Year = 1817, AuthorId = 1, 
        Price = 12.95M, Genre = "Gothic parody" },
    new Book() { Id = 3, Title = "David Copperfield", Year = 1850, AuthorId = 2, 
        Price = 15, Genre = "Bildungsroman" },
    new Book() { Id = 4, Title = "Don Quixote", Year = 1617, AuthorId = 3, 
        Price = 8.95M, Genre = "Picaresque" }
    );
}