C# 将DB中的字段值填充为数据列以查看MVC

C# 将DB中的字段值填充为数据列以查看MVC,c#,asp.net-mvc,C#,Asp.net Mvc,我有以下两个表格: 此表包含以下列: 此表包含链接到这些列的值: 我希望它在我的视图中采用以下格式: 我将其放入一个IENumerable中,并通过我的模型传递给视图: public class BudgetEntryNotesVM { public string AccountNumber { get; set; } public string AccountDescription { get; set; } public IEnumerable<Budge

我有以下两个表格:

此表包含以下列:

此表包含链接到这些列的值:

我希望它在我的视图中采用以下格式:

我将其放入一个IENumerable中,并通过我的模型传递给视图:

public class BudgetEntryNotesVM
{
    public string AccountNumber { get; set; }
    public string AccountDescription { get; set; }
    public IEnumerable<BudgetNoteLineEntryVM> BudgetNoteLineEntryList { get; set; }

}

public class BudgetNoteLineEntryVM
{
    public int pkiNotesLineEntriesId { get; set; }
    public int fkiAccountId { get; set; }
    public string NotesColumnName { get; set; }
    public string Value { get; set; }
    public bool isNewLineItem { get; set; }
    public int Sequence { get; set; }
}
我实在想不出一个解决办法,我希望有人能把我引向正确的方向


谢谢。

如果你有8个条目的序列,没有跳过的数字,你可以这样做

您的数据实体可能看起来像

public class TableEntry
{
    public int pkiNotesLineEntriesId { get; set; }
    public int fkiAccountId { get; set; }
    public int fkiNotesColumnId { get; set; }
    public string Value { get; set; }
    public bool isNewEntry { get; set; }
}
因此,您可以将表数据划分为数组列表:

List<TableEntry[]> desiredFormat = new List<TableEntry[]>();

int pages = entries.Count() / 8;
for (int page = 0; page < pages; page++)
{
    TableEntry[] row = entries.OrderBy(e => e.pkiNotesLineEntriesId).Skip(page * 8).Take(8).ToArray();
    desiredFormat.Add(row);
}
List desiredFormat=new List();
int pages=entries.Count()/8;
对于(int page=0;pagee.pkiNotesLineEntriesId).Skip(第8页).Take(8).ToArray();
desiredFormat.Add(行);
}
然后用一个标题数组将其传递给视图,并将它们分别呈现到表中

如果它没有特定的尺寸,你可以使用这个

int maxSize = entries.Max(c => c.fkiNotesColumnId);
TableEntry[] row = new TableEntry[maxSize];
int current = 0;
foreach (var entry in entries)
{
    if (entry.fkiNotesColumnId < current)
    {
        desiredFormat.Add(row);
        row = new TableEntry[maxSize];
        current = 0;
    }
    row[entry.fkiNotesColumnId - 1] = entry;
    current = entry.fkiNotesColumnId;
}
int-maxSize=entries.Max(c=>c.fkiNotesColumnId);
TableEntry[]行=新TableEntry[maxSize];
int电流=0;
foreach(分录中的var分录)
{
if(entry.fkiNotesColumnId<当前值)
{
desiredFormat.Add(行);
行=新表格条目[maxSize];
电流=0;
}
行[entry.fkiNotesColumnId-1]=条目;
当前=entry.fkiNotesColumnId;
}

多亏了Andrey的帮助,我使用了2D阵列。我这样做也许不是最好的解决办法,但它奏效了:

视图模型:

public class BudgetEntryNotesVM
{
    public string AccountNumber { get; set; }
    public string AccountDescription { get; set; }
    public IEnumerable<BudgetNoteLineEntryVM> BudgetNoteLineEntryList { get; set; }
    public IEnumerable<BudgetNoteLineEntryColumnsVM> BudgetNoteLineEntryColumnsList { get; set; }
    public string[,] BudgetLineEntryArray { get; set; }
}
我的看法是:

@model BudgetEntryNotesVM

<div id="listofBudgetEntries">
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <h3 class="panel-title">Note Line Entries</h3>
            </div>

            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-condensed table-bordered table-hover">
                        <tr style="background-color: #00b8ce; color: white;">
                            @foreach (var hlitem in Model.BudgetNoteLineEntryColumnsList)
                            {
                                <th>
                                    @Html.DisplayFor(modelhitem => hlitem.ColumnName)
                                </th>


                            }
                        </tr>

                        @for (int row = 0; row <= Model.BudgetLineEntryArray.GetUpperBound(0); row++)
                        {
                            <tr>
                                @for (int column = 0; column <= Model.BudgetLineEntryArray.GetUpperBound(1); column++)
                                {
                                    <td>@Model.BudgetLineEntryArray[row, column]</td>
                                }
                            </tr>
                        }
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@model BudgetEntryNotesVM
注释行条目
@foreach(Model.BudgetNoteLineEntryColumnsList中的var hlitem)
{
@DisplayFor(modelhitem=>hlitem.ColumnName)
}

@为了(int row=0;row抱歉,我本应该显示包含我从中提取的数据的两个表。两者之间的链接是fkiNotesColumnId-我相信这是标识哪些值属于哪列的字段?希望这能帮上一点忙?@AxleWack您可以创建一个二维数组,其中所有数据都将按顺序排序。)d将其与第一个表中的标题分开呈现。
ViewModel
应包含已格式化的数据。我将查看并尝试一下。感谢AndreyTanks Stephen-这似乎更符合我的意愿,唯一的问题是属性“Grade”、“BasicPay”、“加班时间”等不是静态字段,它们可能是静态字段用户可以选择任何内容。有什么办法解决这个问题吗?感谢Stephen的回复。它可以少也可以多。我目前已经放了8列-这取决于用户想要多少列。它可以是3列,也可以是5列,也可以是10列,这都取决于用户。我已经在下面发布了代码,并且能够通过使用2D数组使其工作。我确信有一个更好的方法,但这对我来说暂时有效,它可以大于或小于8,但序列应该始终按顺序运行。我完全没有想法,所以请分享更多信息,我将尝试查看它。不幸的是,上述方法无法正常工作,因为fkiNotesColumnId的顺序可能不同,这取决于列的顺序。但是,您确实通过使用2D数组将我引向了正确的方向!因此我对此表示感谢!我将发布我的代码以供大家查看。
public class BudgetEntryNotesVM
{
    public string AccountNumber { get; set; }
    public string AccountDescription { get; set; }
    public IEnumerable<BudgetNoteLineEntryVM> BudgetNoteLineEntryList { get; set; }
    public IEnumerable<BudgetNoteLineEntryColumnsVM> BudgetNoteLineEntryColumnsList { get; set; }
    public string[,] BudgetLineEntryArray { get; set; }
}
//Getting Columns to populate table in PartialView
    public IEnumerable<BudgetNoteLineEntryColumnsVM> GetBudgetNoteEntryLineColumns(int accId)
    {
        IEnumerable<BudgetNoteLineEntryColumnsVM> AccountsLineEntriesColumnIList = new List<BudgetNoteLineEntryColumnsVM>();

        //var query2 =(from )

        var query = (from NC in _context.Notes_Columns
                     join NLE in _context.Notes_Line_Entries on NC.pkiNotesColumnsId equals NLE.fkiNotesColumnId
                     where NLE.fkiAccountId == accId
                     select new BudgetNoteLineEntryColumnsVM
                     {
                         Sequence = NC.Sequence,
                         ColumnName = NC.NotesColumnName

                     }).Distinct().OrderBy(n=>n.Sequence);

        AccountsLineEntriesColumnIList = query;

        return AccountsLineEntriesColumnIList;
    }

//Getting data to populate table
    public string[,] GetBudgetNoteLineEntry(int accId)
    {
        int row = 0;
        int column = 0;

        //Getting total number of rows based on the EntryLineId
        var outer = (from NC in _context.Notes_Columns
                     join NLE in _context.Notes_Line_Entries on NC.pkiNotesColumnsId equals NLE.fkiNotesColumnId
                     where NLE.fkiAccountId == accId
                     select NLE.EntryLineId).Distinct().Count();

        //Getting total number of columns based on the Account ID
        var inner = (from NC in _context.Notes_Columns
                     join NLE in _context.Notes_Line_Entries on NC.pkiNotesColumnsId equals NLE.fkiNotesColumnId
                     where NLE.fkiAccountId == accId
                     select NC.NotesColumnName).Distinct().Count();

        //Declaring 2D Array with Inner and Outer values above
        string[,] entrylines = new string[outer, inner];

        //Getting all data in order of the Columns
        var query = (from NC in _context.Notes_Columns
                     join NLE in _context.Notes_Line_Entries on NC.pkiNotesColumnsId equals NLE.fkiNotesColumnId
                     where NLE.fkiAccountId == accId
                     select new BudgetNoteLineEntryVM
                     {
                         pkiNotesLineEntriesId = NLE.pkiNotesLineEntriesId,
                         fkiAccountId = NLE.fkiAccountId,
                         NotesColumnName = NC.NotesColumnName,
                         Value = NLE.Value,
                         isNewLineItem = NLE.isNewEntry,
                         Sequence = NC.Sequence,
                         EntryLineId = NLE.EntryLineId

                     }).Distinct().OrderBy(n => n.Sequence).ThenBy(n=>n.EntryLineId);

        //Looping through every item and adding to 2D array based on the ordering of the columns
        foreach(var item in query)
        {
            if(row >= outer)
            {
                row = 0;
                column++;
            }

            entrylines[row, column] = item.Value;

            if (row <= outer)
            {
                row++;
            }

        }

        return entrylines;
    }
public ActionResult BudgetNoteLineEntry(int accId)
    {
        BudgetEntryNotesVM NVM = new BudgetEntryNotesVM();

        NVM.AccountNumber = _budgetEntryRepository.GetAccountName(accId);
        NVM.AccountDescription = _budgetEntryRepository.GetAccountDescription(accId);

        NVM.BudgetNoteLineEntryColumnsList = _budgetEntryRepository.GetBudgetNoteEntryLineColumns(accId);
        //NVM.BudgetNoteLineEntryList = _budgetEntryRepository.GetBudgetNoteLineEntry(accId);
        NVM.BudgetLineEntryArray = _budgetEntryRepository.GetBudgetNoteLineEntry(accId);

        return PartialView("_ShowAccountBudgetLineEntries", NVM);
    }
@model BudgetEntryNotesVM

<div id="listofBudgetEntries">
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <h3 class="panel-title">Note Line Entries</h3>
            </div>

            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-condensed table-bordered table-hover">
                        <tr style="background-color: #00b8ce; color: white;">
                            @foreach (var hlitem in Model.BudgetNoteLineEntryColumnsList)
                            {
                                <th>
                                    @Html.DisplayFor(modelhitem => hlitem.ColumnName)
                                </th>


                            }
                        </tr>

                        @for (int row = 0; row <= Model.BudgetLineEntryArray.GetUpperBound(0); row++)
                        {
                            <tr>
                                @for (int column = 0; column <= Model.BudgetLineEntryArray.GetUpperBound(1); column++)
                                {
                                    <td>@Model.BudgetLineEntryArray[row, column]</td>
                                }
                            </tr>
                        }
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>