Bram
Bram

Reputation: 739

How do I pass multiple different DB objects to the same View in different tables

For a school project I'm making an application in ASP.net Core using Entity Framework. For this application I have to execute 5 different checks on orders done with SQL, the orders that match the criteria of the sql have to be placed in a table. The results have to placed on the same view in a different table corresponding to the triggered check.

Im using 2 models: Alerts and Orders where I have joined Alerts and Orders so I can get the OrderID matching a Alert (Check)

My application consists of a EFRepository containing the following methods:

public IEnumerable<Alerts> GetAlertsByCheck(int checkNumber)
{
    IEnumerable<Alerts> temp = null;
    switch (checkNumber)
    {
        case 1:
            temp = DB.Alerts.Where(e => e.Controle.Equals(CHECK_ONE));
            break;
        case 2:
            temp = DB.Alerts.Where(e => e.Controle.Equals(CHECK_TWO));
            break;
        case 3:
            temp = DB.Alerts.Where(e => e.Controle.Equals(CHECK_THREE));
            break;
        case 4:
            temp = DB.Alerts.Where(e => e.Controle.Equals(CHECK_FOUR));
            break;
        case 5:
            temp = DB.Alerts.Where(e => e.Controle.Equals(CHECK_FIVE));
            break;
    }

    return temp;
}

Getting the Orders for each Alert

public IEnumerable<Orders> GetOrdersByAlert(IEnumerable<Alerts> alerts)
{
    IEnumerable<Orders> temp = null;
    List<Orders> list = new List<Orders>();
    foreach (var alert in alerts)
    {
        Orders order = DB.Orders.First(e => e.Id == alert.Id);
        list.Add(order);
    }

    temp = list;
    return temp;
}

In the following view im trying to place the OrderID of each check in a seperate table. So Table 1 should contain the OrderID's where the check 1 triggered, table 2 should contain the OrderID's where check 2 triggered.

@using Domain.Models.TableModels
@model IEnumerable<Domain.Models.TableModels.Orders>


@{
    ViewData["Title"] = "ResolvedIssuesOverview";
}

<!-- Check 1 table-->
<div class="row">
    <h2>Resolved Issues Overview</h2>
    <div class="col-md-12">
        <h3>Check 1</h3>
        <p>Total amount of orders from the last 72h is higher than &euro; 100.000</p>
        <table class="table table-striped">
            <tr>
                <th>Order ID</th>
                <th>Status</th>
            </tr>
            @foreach (var order in Model ?? Enumerable.Empty<Orders>())
            {
                <tr>
                    <td>@order.Id</td>
                    <td>Resolved</td>
                </tr>
            }

        </table>
    </div>
    <!-- Check 2-->
    <div class="col-md-12">
        <h3>Check 2</h3>
        <p>Country is in FATF high risk list</p>
        <table class="table table-striped">
            <tr>
                <th>Order ID</th>
                <th>Status</th>
            </tr>
            @foreach (var order in Model ?? Enumerable.Empty<Orders>())
            {
                <tr>
                    <td>@order.Id</td>
                    <td>Resolved</td>
                </tr>
            }
        </table>
    </div>

Now in the HomeController I have gave the method a id=2 parameter for testing, and it succesfully placed the OrderID's from Check_two in the table.

public ViewResult ResolvedIssuesOverview(int id2=2)
{           
    return View(AlertsRepository.GetOrdersByAlert(AlertsRepository.GetSolvedAlertsByCheck(id2)));           
}

Now my problem is that I can't figure out how I can pass the other checks to the view and place them in their own table.

To better illustrate my question the tables in the view look like this:

+---------+------------------------+
| OrderID |         Check          |
+---------+------------------------+
|       1 | Invalid payment method |
|       2 | Invalid payment method |
+---------+------------------------+

+---------+------------------------+
| OrderID |         Check          |
+---------+------------------------+
|       3 | Payment aborted        |
|       4 | Payment aborted        |
+---------+------------------------+

Any help would be greatly appreciated! Thanks in advance :)

Upvotes: 1

Views: 112

Answers (1)

DaveB
DaveB

Reputation: 9530

Make a viewmodel class and pass it to the view populated with the 2 tables.

public class OrdersViewModel
{
     public IEnumerable<Orders> CheckOneOrders { get;set; }
     public IEnumerable<Orders> CheckTwoOrders { get; set; }
}

Then access each table as needed in the view.

@model OrdersViewModel

You will need to write the code to instantiate and populate the 2 properties on the view model.

Upvotes: 1

Related Questions