簡體   English   中英

列出未傳遞到視圖的項目

[英]List items not being passed to the view

對於我的小型Web應用程序項目,我創建了一個名為Company的模型,其中包含基本的公司信息以及不同業務合作伙伴的銷售代表列表。 這是我的聯系方式:

 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();

如何將聯系人列表項加載到剃刀視圖上? 我正在查看我的數據庫,它顯示“聯系人”的空白字段。 其余數據顯示正常。

參見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");
}

輸出:

你好,世界

保存完成。

公司數:2

公司[0]。名稱:某些名稱

公司[0]。聯系人[0]。聯系人姓名:John Doe

company [1]。名稱:另一個名稱

公司[1]。聯系人[0]。聯系人姓名:Jane Doe

再見世界

嗨,在我的最后一個項目中,我對viewmodel進行了相同的操作,因為它是來自兩個不同表的數據,並為將來的更改提供了更大的自由。

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

什么是MVC中的ViewModel?

因此,根據您的描述,聽起來好像您正在接收模型-但Contacts集合為null或為空。

這是因為EntityFrameworkCore需要使用上下文Db Set上的Include顯式Include子集。 瀏覽此處獲取更多信息

下面的代碼是一個示例,該示例說明如何在加載時將子集合包含在模型中

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM