Answer the question
In order to leave comments, you need to log in
How to output complex data to ASP.Net MVC5 view?
I have a "Discounts" table, which contains foreign keys for customers and materials (2 different tables), but the problem is that I can't display the data in the view.
For example, the "Payments" page has only a key for customers , in it it turns out as:
DatabaseContext db = new DatabaseContext();
public ActionResult Payments()
{
var payments = db.Payments.Include(p => p.Customer);
return View(payments.ToList());
}
@foreach (var f in Model)
{
<tr class="content">
<td><p>@f.Id</p></td>
<td><p>@f.Date</p></td>
<td><p>@f.Customer.CustomerName</p></td>
</tr>
}
DatabaseContext db = new DatabaseContext();
public ActionResult Sales()
{
//Что сюда писать?
return View();
}
public class Sale
{
public int Id { get; set; }
public int? CustomerId { get; set; }
public Customer Customer { get; set; }
public int? MaterialId { get; set; }
public Material Material { get; set; }
public float Changes { get; set; }
}
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DatabaseContext : DbContext
{
public DatabaseContext() : base("conn")
{ }
//Non-inheriting
public DbSet<Customer> Customers { get; set; }
public DbSet<Material> Materials { get; set; }
//Other
public DbSet<DeliveryAddress> DeliveryAddresses { get; set; }
public DbSet<Payment> Payments { get; set; }
public DbSet<Sale> Sales { get; set; }
}
Answer the question
In order to leave comments, you need to log in
Use composition, roughly speaking, select data from one and the other and insert this data into the model, because the page model does not have to match the data model, use automapper for mapping
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question