A
A
authoraytee2020-05-25 16:04:27
MySQL
authoraytee, 2020-05-25 16:04:27

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

And in the view
@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>
}


Actually the question is how to bring both customers and materials to the page with discounts?

Controller
DatabaseContext db = new DatabaseContext();

public ActionResult Sales()
{ 
    //Что сюда писать?
    return View();
}


Model
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; }
    }


Context
[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

1 answer(s)
V
Vladimir Korotenko, 2020-05-25
@authoraytee

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 question

Ask a Question

731 491 924 answers to any question