2012-04-06 17 views
1

我想通过使用两个外键在实体中显示两个值。MVC3无法显示其他表值使用外键

我有三张表;表中的一个是产品表。

两个表格分别是Category和Model,用于显示这些值'name'和'modelName'。

当我使用LINQ时,我在添加Model实体之前使用了此编码。

var product = from a in db.Product.Include(a => a.Category) 
         select a; 

如何在此处添加Model实体?

var product = from a in db.Product.Include(a => a.Category, a => a.Model) 
         select a; 

是否可以写?

这是我的模特。

--Prodruct.cs-- 

public class Product 
{ 
    [Key] public int productId { get; set; } 

    [Required(ErrorMessage = "Please select category")] 
    public int categoryId { get; set; } 

    [Required(ErrorMessage = "Please select model")] 
    public int modelId { get; set; } 

    [DisplayName("Model name")] 
    public String model { get; set; } 

    public virtual Category Category { get; set; } 
    public virtual Model Model { get; set; } 
} 

--Category.cs-- 
public class Category 
{ 
    [Key] public int categoryId { get; set; } 
    public String name { get; set; } 
} 

--Model.cs-- 
public class Model 
{ 
    [Key] public int modelId { get; set; } 
    public String name { get; set; } 
} 

--RentalDB.cs-- 
public class rentalDB : DbContext 
{ 
    public DbSet<Product> Product { get; set; } 
    public DbSet<Model> Model { get; set; } 
    public DbSet<Customer> Customer { get; set; } 
    public DbSet<Order> Order { get; set; } 
    public DbSet<Cart> Cart { get; set; } 
    public DbSet<Category> Category { get; set; } 
    public DbSet<OrderDetails> OrderDetails { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    } 
} 

请让我知道如何将内连接(?)放在LINQ中。

谢谢。

回答

1

我想你可能需要以下的自包含返回的IQueryable:

var product = from a in db.Product.Include(a => a.Category).Include(a => a.Model) 
        select a; 
+0

为wholee1只是一个点:“LINQ”生成左边加入了包括不内。如果你想检查是否存在关系,你将不得不使用一些“where” – 2012-04-06 12:20:05

+0

谢谢你的回复,但是当我把你的编码,我在'返回视图(product.ToList());错误';'它说'元数据集合中的多个项目与'模型'的标识相匹配。是否有可能在linq中使用两个实体类别和模型? – wholee1 2012-04-06 13:21:42

0

这是你在你的ProductController.cs需要什么...

public ViewResult index(int param_categoryId, int param_modelId) 
    { 
     List<Product> locvar_CollectionOfProduct 
      = getCollectionOfProduct(param_categoryId, param_modelId); 

     return View("index", locvar_CollectionOfProduct); 
    } 

    private List<Product> getCollectionOfProduct(int param_categoryId, int param_modelId) 
    { 
     return db.Product.Where(a => a.categoryId == param_categoryId && a.modelId == param_modelId).ToList(); 
    } 

    public void Product_Save(List<Product> param_CollectionOfProduct) 
    { 
     if (Model.IsValid) 
     { 
      foreach (Product i_Product in param_CollectionOfProduct) 
      { 
       Product locvar_Product = null; 

       if (i_Product.productId == null || i_Product.productId == 0) 
       { 
        locvar_Product = new Product(); 
       } 
       else 
       { 
        locvar_Product = new Product{productId = i_Product.productId}; 
        db.Product.Attach(locvar_Product) 
       } 

       locvar_Product.categoryId = i_Product.categoryId; 
       locvar_Product.modelId = i_Product.modelId; 

       if (i_Product.productId == null || i_Product.productId == 0) 
       { 
        db.Product.Add(locvar_Product); 
       } 
      } 

      db.SaveChanges(); 
     } 
    } 

,然后在“查看\ Product \ index.cshtml“视图,你可以遍历这些。我会把它们放在你的桌子上:

@using insert_entity_reference_here.Models; 
    @model List<Product> 

    @{ 
     List<Product> param_CollectionOfProduct = Model; 
    } 

    @using (Ajax.BeginForm("Product_Save", "Product", null, new AjaxOptions { HttpMethod = "POST" })) 
    { 

    <table style="width:100%"> 

     <tr> 
      <th> 
       Category Name 
      </th> 
      <th> 
       Model Name 
      </th> 
     </tr> 

     @if(Model.Count() > 0) 
     { 

      for(i_Product = 0 ; i_Product < Model.Count() ; i_Product++) 
      { 

       @Html.HiddenFor(modelItem => param_CollectionOfProduct[i_Product].productId) 

       <tr> 
        <td> 
         @Html.HiddenFor(modelItem => param_CollectionOfProduct[i_Product].Category.categoryId) 
         @Html.EditorFor(modelItem => param_CollectionOfProduct[i_Product].Category.Name, new { style="width:100%" }) 
         @Html.ValidationMessageFor(modelItem => param_CollectionOfProduct[i_Product].Category.Name) 
        </td> 
        <td> 
         @Html.HiddenFor(modelItem => param_CollectionOfProduct[i_Product].Model.modelId) 
         @Html.EditorFor(modelItem => param_CollectionOfProduct[i_Product].Model.Name, new { style="width:100%" }) 
         @Html.ValidationMessageFor(modelItem => param_CollectionOfProduct[i_Product].Model.Name) 
        </td> 
       </tr> 
      } 
     } 

    </table> 

    <input type="submit">Save</input> 

    } 

让我知道,如果我在正确的轨道上。如果是这样,我应该能够帮助你更多。

最好的问候, 尼克