简体   繁体   English

Asp.net初学者问题

[英]Asp.net beginner issues

So I'm attempting to write a practice asp.net website for a bike store network. 因此,我试图为自行车商店网络编写一个练习asp.net网站。 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 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. 但是,它会打印出“从0个商店中选择”,然后打印出来。 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. 我很乐意提供您认为相关的任何其他项目文件。

You aren't populating the entities, your local variable goes out of scope and no data is added. 您没有填充实体,您的局部变量超出了范围,并且未添加任何数据。

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

See the example from here on how to seed: http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-3 请参见此处的播种示例: 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" }
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM