使用与另一个表的关系创建CRUD

时间:2014-01-16 21:38:56

标签: asp.net-mvc asp.net-mvc-4 crud

我在创建CRUD控制器时遇到问题。我有自行车模型

namespace BikeShop2.Models
{
    public class Bike
    {
        public long Id { get; set; }
        public decimal Price { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string PictURL { get; set; }
        public Manufacturer Manufacturer { get; set; }
        public Equipment Equipment { get; set; }
    }
}

我的问题是CRUD不包含制造商和设备,即使只列出它。但在另一个控制器中它工作正常。可能是什么问题? 我的控制器样本。在Details方法中,所需属性均为null。

public class StoreController : Controller
{

    BikeShopEntities shopDB = new BikeShopEntities();
    //
    // GET: /Store/
    public ActionResult Index()
    {
        var manufacturers = shopDB.Manufacturers.ToList();
        return View(manufacturers);
    }
    //
    // GET: /Store/Browse?genre=Disco
    public ActionResult Browse(string name)
    {
        var man = shopDB.Manufacturers.Include("Bikes")
            .Single(g => g.Name == name);


        return View(man);
    }
    //
    // GET: /Store/Details/5
    public ActionResult Details(int id)
    {
        var bike = shopDB.Bikes.Find(id);
        return View(bike);
    }
}

}

这是我的SampleData.cs文件

public class SampleData : DropCreateDatabaseIfModelChanges<BikeShopEntities>
{
    protected override void Seed(BikeShopEntities context)
    {
        var manufacturers = new List<Manufacturer>
        {
            new Manufacturer { Name = "Orbea" },
            new Manufacturer { Name = "Lapierre" },
            new Manufacturer { Name = "Romet" },
            new Manufacturer { Name = "TREK" },
            new Manufacturer { Name = "Conandale" },
            new Manufacturer { Name = "Specialized" },
            new Manufacturer { Name = "AUTHOR" },
            new Manufacturer { Name = "Cervelo" },
            new Manufacturer { Name = "Kross" },
            new Manufacturer { Name = "FELT" }
        };

        var equipments = new List<Equipment>
        {
            new Equipment { Name = "Shimano" },
            new Equipment { Name = "SRAM" },
            new Equipment { Name = "Campagnolo" },
            new Equipment { Name = "NONAME" },
            new Equipment { Name = "ACCENT" },
            new Equipment { Name = "Easton" },   
        };

        new List<Bike>
        {
            new Bike { Name = "Orca", Manufacturer = manufacturers.Single(g => g.Name         == "Orbea"), Price = 9000.00M, Equipment = equipments.Single(a => a.Name == "Campagnolo"), PictURL = "/Content/Images/placeholder.gif" },
            new Bike { Name = "Aqua", Manufacturer = manufacturers.Single(g => g.Name == "Orbea"), Price = 4000.00M, Equipment = equipments.Single(a => a.Name == "Campagnolo"), PictURL = "/Content/Images/placeholder.gif" },
        }.ForEach(a => context.Bikes.Add(a));
    }
}

0 个答案:

没有答案