2017-04-26 40 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

谢谢,有一点! –