2017-04-26 37 views
0

因爲我已經創建了一個模式叫公司,包括公司基本信息&還銷售代表從不同的業務合作伙伴名單我小的web應用程序項目。這是我的聯繫型號:列表項沒有傳遞到視圖

public class Company 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string Promo { get; set; } // Yes or No field 
     public List<Contact> Contacts { get; set; } 
     public class Contact 
     { 
      [Key] 
      public int ContactID { get; set; } 
      public int CompanyID { get; set; } 
      public string ContactName { get; set; } 
      public string ContactNumber { get; set; } 
     } 
    } 

這是我如何將數據傳遞到我的本地數據庫:

var companiesData = new Company[] 
     { 
      new Company 
      { 
      Name = "Some name", Promo="Y", Contacts = new List<Contact> { new Contact {ContactName="John Doe", ContactNumber="(828)292-2912", CompanyID=1}, 
      }}, // ... some more contacts 
     }; 
     foreach (Company c in companiesData) 
     { 
      context.Companies.Add(c); 
     } 
     context.SaveChanges(); 

如何加載聯繫人列表中的項目到剃刀看法?我在看我的數據庫,它顯示「聯繫人」的空白字段。其餘的數據顯示出來很好。

+0

您節省看起來不正確,'context.Companies.Add(C)'應該是'CON text.Contacts.Add(C)'。而你需要先保存新的'Company'讓你有一個'CompanyID'保存與接觸 – user326608

+0

'Contacts'屬性是一個'名單'所以它不是映射到現場,而是在'聯繫人的外鍵'與'公司'的'ID'匹配的表。你認爲它是什麼「空白領域」? – granit

+0

在SQL對象資源管理器的dbo.Companies表中。列聯繫人爲空白,其餘數據爲數據(從同一對象傳入) –

回答

1

所以根據您的描述,這聽起來像您收到的模型 - 但聯繫人集合爲null或空。

這是因爲EntityFrameworkCore需要孩子套在你的背景DB中設置使用Include被明確列入。 See here for more information

下面的代碼是如何在您的模型預先加載

public class CompanyController : Controller 
{ 
    // This import is needed for using the 'Include' method. 
    using Microsoft.EntityFrameworkCore; 

    private ApplicationDbContext _context; 
    public CompanyController(ApplicationDbContext context) 
    { 
     _context = context; 
    } 
    // GET: /<controller>/ 
    public IActionResult Index() 
    { 
     List<Company> viewModelData = _context.Companies.Include(p => p.Contacts).ToList(); 

     return View(viewModelData); 
    } 
} 

public class Company 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Promo { get; set; } // Yes or No field 
    public List<Contact> Contacts { get; set; } 
    public class Contact 
    { 
     [Key] 
     public int ContactID { get; set; } 
     public int CompanyID { get; set; } 
     public string ContactName { get; set; } 
     public string ContactNumber { get; set; } 
    } 
} 

,並且數據庫上下文

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
     : base(options) 
    { 
    } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 
    } 

    public DbSet<Company> Companies { get; set; } 
    public DbSet<Contact> Contacts { get; set; } 
} 

查看文件子集應設置模型,像這樣

爲例
@model List<Company> 

<h3>Do something with 'Model' here</h3> 
+1

這工作。謝謝! –

+0

請在必要時將其標記爲答案,很高興它的工作。 – ColinM

+1

任何讀取數據都必須使用'Include',創建數據應該沒問題,因爲您要插入收集並保存。根據您如何刪除數據,更新和刪除可能需要一個「Include」。 – ColinM

2

看到https://dotnetfiddle.net/xb3g68

實體:

public class Company 
{ 
    [Key] 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Promo { get; set; } 
    public virtual List<Contact> Contacts { get; set; }  
} 

public class Contact 
{ 
    [Key] 
    public int ContactID { get; set; } 
    [ForeignKey("Company")] 
    public int CompanyID { get; set; } 
    public virtual Company Company { get; set; } 
    public string ContactName { get; set; } 
    public string ContactNumber { get; set; } 
} 

測試工具:

public static void Main() 
{ 
    Console.WriteLine("Hello World"); 
    FakeDbContext context = new FakeDbContext(); 
    var companiesData = new Company[] 
    { 
     new Company 
     { 
      Name = "Some name", Promo="Y", Contacts = new List<Contact> { new Contact {ContactName="John Doe", ContactNumber="(828)292-2912", CompanyID=1}}, 
     }, 
     new Company 
     { 
      Name = "Another name", Promo="N", Contacts = new List<Contact> { new Contact {ContactName="Jane Doe", ContactNumber="(828)292-2912", CompanyID=2}}, 
     }, 
    }; 
    foreach (Company c in companiesData) 
    { 
     context.Companies.Add(c); 
     foreach (Contact contact in c.Contacts) 
     { 
      context.Contacts.Add(contact); 
     } 
    } 
    context.SaveChanges(); 
    Console.WriteLine("Save done."); 
    var companies = context.Companies.ToList(); 
    Console.WriteLine("companies.Count: " + companies.Count); 
    for (int i = 0; i < companies.Count; i++) 
    { 
     Console.WriteLine(string.Format("company[{0}].Name: {1}", i,  companies[i].Name)); 
     for (int j = 0; j < companies[i].Contacts.Count; j++) 
     {    
Console.WriteLine(string.Format("company[{0}].Contacts[{1}].ContactName: {2}", i, j, companies[i].Contacts[j].ContactName)); 
     } 
    } 
    Console.WriteLine("Bye World"); 
} 

輸出:

的Hello World

保存完成。

companies.Count:2

公司[0]請將.Name:有些名字

公司[0] .Contacts [0] .ContactName:李四

company [1] .Name:另一個名字

公司[1] .Contacts [0] .ContactName:李四

再見世界

+0

你有沒有現在的數據保存到你的分貝 - 所以聯繫等與公司參考集? – user326608

+0

感謝您向我展示如何測試我的代碼! –

+1

只是要小心使用ID值 - 與針對你可能會想'數據庫的正確EF提供商[DatabaseGenerated(DatabaseGeneratedOption.Identity)]'下面設置你的'[關鍵]'屬性,EF將創建上下文ID值加。然後你會使用它作爲外鍵。請注意,在調用SaveChanges之前,db不會更新。上面有關使用'.Include()'的註釋也是有用的。如果你使用的是.NET Core,可以玩一個SQLite InMemory教程項目 - 我試圖讓dotnetfiddle運行.NET.4.5版本的SQLite和Effort而沒有結果 – user326608

2

你好我的最後一個項目,我做了同樣的與視圖模型,因爲是兩個不同表中的數據,並給予更多的自由要改變認爲在future.So我創造出一個視圖模型

public class ViewModelContactCompany 
{ 
    public String Name { get; set; } 
    List<Contact> contacts { get; set; } 
    //etc just a sample 
} 

控制器

public class Controler 
{ 
//param id is a id from company 
//to do a relationship 
public Action ControlerDetailsContact(int? id) 
{ 
    ViewModelContactCompany x = new ViewModelContactCompany(); 
    //Do a linq, sqlquery,etc 

    x.Name = "sample"; //get name of company by id; 

    for (;;) 
    { 
     //where id = contact.CompanyID 
     //add a new object typeof contact to the viewmodel 
     //with the data get from the relationship 
     x.contacts.Add(new Contact()); 
    } 

    //and in the final return object viewmodel to the view 
    return View(x); 
} 

}

現在查看

@model /patch.ViewModelContactCompany 

and here you get the data on the object return on the controler 

What is ViewModel in MVC?

+0

謝謝,有一點! –