如何显示链接到用户列的表中的金额

How can I show amounts from a table linking to columns of users

提问人:allen.sophie 提问时间:8/19/2023 最后编辑:marc_sallen.sophie 更新时间:8/20/2023 访问量:84

问:

我有两个表,一个用户和一个事务。

Users             Transactions

| Id | Name  |   | Id | TranId  |UserId  | Amount |
| -- | ------|   | -- | ------  |------  | ------ |
| 1  | John  |   | 1  | 1       | 2      | -100   |
| 2  | Jane  |   | 2  | 1       | 1      |  100   |

我有一个表格,我想在其中显示每笔交易并显示每个用户的金额。

我可以对交易进行分组,但我似乎无法在正确的列中显示金额。

使用我当前的代码,在 Jane 列下它显示 100,但对于任何其他用户,它只显示“N/A”

我的控制器

using (var context = new SharedExpensesEntities())
{
    transactionsList = context.Transactions.ToList();
    usersList = context.Users.ToList();
}

var viewModel = new TransactionListModel
        {
            TransactionList = transactionsList.GroupBy(x => x.TranId)
                .Select(g => new Transaction
                {
                    Id = g.LastOrDefault().Id,
                    TranId = g.Key,
                }).ToList(),

            UserList = usersList
        };

foreach (var transaction in viewModel.TransactionList)
{
    foreach (var user in viewModel.UserList)
    {
        var userTransaction = transactionsList.FirstOrDefault(t => t.UserId == user.Id && t.TranId == transaction.TranId);

        if (userTransaction != null)
        {
            transaction.UserId = user.Id;
            transaction.Amount = userTransaction.Amount;
        }
    }
}

我的cshtml:

<table class="table">
    <tr class="table-header">
        <th>
            Id
        </th>
        <th>
            TransId
        </th>
        @foreach (var user in Model.UserList)
        {
            <th>
                @user.Name
            </th>
        }
    </tr>
    @foreach (var item in Model.TransactionList)
    {
        <tr class="user-item" data-bs-toggle="modal" data-bs-target="#editUserModal">
            <th>@item.Id</th>
            <th>@item.TranId</th>

            @foreach (var user in Model.UserList)
            {
                <th>@(Model.TransactionList.FirstOrDefault(t => t.UserId == user.Id)?.Amount != null ? string.Format("{0:N2}", Model.TransactionList.FirstOrDefault(t => t.UserId == user.Id).Amount) : "N/A")</th>
            }
        </tr>
    }
</table>
sql-server asp.net-mvc Razor Web 应用程序

评论

0赞 Kuldeep 8/20/2023
您能否添加您希望在网页上看到的预期结果?

答:

0赞 Dave B 8/20/2023 #1

生成

一个表格,我想在其中显示每笔交易并显示每个用户的金额

TranId John
1 -100 100

您的代码可以简化。下面使用相同的代码常规结构。

从变量开始:viewModel

viewModel = new TransactionListViewModel()
{
    GroupedTransactionList = transactionsList.GroupBy(tr => tr.TranId),
    UserList = usersList
};

其中类定义为:TransactionListViewModel

public class TransactionListViewModel
{
    public required IEnumerable<IGrouping<int, Transaction>> GroupedTransactionList { get; set; }
    public required IEnumerable<User> UserList { get; set; }
}

.cshtml视图

<table class="table">
    <thead>
        <tr class="table-header">
            <th>
                TransId
            </th>
            @foreach (var user in Model.UserList)
            {
                <th>
                    @user.Name
                </th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (IGrouping<int, Transaction> trGroup in Model.GroupedTransactionList)
        {
            <tr class="user-item" data-bs-toggle="modal" data-bs-target="#editUserModal">
                <td>
                    @trGroup.Key
                </td>
                @foreach (User user in Model.UserList)
                {
                    int? amount = trGroup.FirstOrDefault(tr => tr.UserId == user.Id)?.Amount;
                    <td>
                        @(amount.HasValue ? string.Format("{0:N2}", amount) : "N/A")
                    </td>
                }
            </tr>
        }
    </tbody>
</table>