簡體   English   中英

Asp.net初學者問題

[英]Asp.net beginner issues

因此,我試圖為自行車商店網絡編寫一個練習asp.net網站。 如果查看我的商店對象的索引視圖以及我的模擬數據庫和控制器,您會發現我打算在該視圖中打印商店的編號和名稱:

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

調節器

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

模型

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

視圖

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

但是,它會打印出“從0個商店中選擇”,然后打印出來。 有誰知道這里會發生什么? 我很樂意提供您認為相關的任何其他項目文件。

您沒有填充實體,您的局部變量超出了范圍,並且未添加任何數據。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM