2015-06-11 92 views
0

這可能是一個愚蠢的問題,因爲我是新來的MVC模式在asp.net。 我試圖訪問我的數據庫中的值,但值在視圖頁上不會呈現 。表值沒有顯示 - ASP.net

這是我寫的代碼。對於 「RESTURANT」 模式

代碼:這裏

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 

namespace OdetoFood.Models 
{ 
    [Table("Resturants")] 
    public class Resturant 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string City { get; set; } 
     public string Country { get; set; } 
     public ICollection<ResturantReviews> Reviews { get; set; } 

    } 
} 

是我的DbContext型號:

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

namespace OdetoFood.Models 
{ 
    public class OdeToFoodDb : DbContext 

    { 
     public DbSet<Resturant> Resturants { get; set; } 
     public DbSet<ResturantReviews> Reviews { get; set; } 
    } 
} 

規範的控制器類:

public class HomeController : Controller 
    { 
     OdeToFoodDb _db = new OdeToFoodDb(); 
     public ActionResult Index() 
     { 
      var model = _db.Resturants.ToList(); 

      return View(model); 
     } 

,這裏是認爲應該顯示值:

@model IEnumerable<OdetoFood.Models.Resturant> 

@{ 
    ViewBag.Title = "Home Page"; 
} 


@foreach (var item in Model) 
{ 
    <h3>@item.Name</h3> 
    <div>@item.City, @item.Country</div> 
    <div>@item.Id</div> 
} 

Web.config中的連接字符串設置這樣的:

<connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-OdetoFood-20150611025411.mdf;Initial Catalog=aspnet-OdetoFood-20150611025411;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

什麼我錯在這裏做什麼?

+0

我熟悉的教程。你是否收到錯誤或者只是沒有數據? Seed()是否是數據庫? –

+0

我沒有收到任何錯誤。視圖頁面呈現正常,但值不顯示。你怎麼做Seed()? – Psyoptica

+0

如果您在製作pluralsight視頻時有數據庫遷移的前面部分。如果您不是,則可以手動填充或創建遷移。 –

回答

0

這將是該項目的種子方法:

protected override void Seed(OdeToFood.Models.OdeToFoodDb context) 

    { 

     context.Restaurants.AddOrUpdate(r => r.Name, 

      new Restaurant { Name = "Sabatino's", City = "Baltimore", Country = "USA" }, 

      new Restaurant { Name = "Great Lake", City = "Chicago", Country = "USA" }, 

      new Restaurant 

      { 

       Name = "Smaka", 

       City = "Gothenburg", 

       Country = "Sweden", 

       Reviews = 

       new List<RestaurantReview>{ 

         new RestaurantReview{ Rating = 9, Body="Great Food!" } 

        } 

      }); 

    }