将数据库中的字段值填充为具有数据的列到View MVC

时间:2017-03-23 12:03:53

标签: c# asp.net-mvc

我有以下2个表格:

此表包含以下列: enter image description here

此表包含链接到这些列的值: enter image description here

我希望在我看来这种格式:

enter image description here

我已将它放入IENumerable中,我将通过我的模型传递给视图:

div

我使用以下内容填充数据:

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

我填充它并以这种方式传递它:

    public IEnumerable<BudgetNoteLineEntryVM> GetBudgetNoteLineEntry(int accId)
    {
        IEnumerable<BudgetNoteLineEntryVM> AccountsLineEntriesIList = new List<BudgetNoteLineEntryVM>();

        //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 BudgetNoteLineEntryVM
                     {
                         pkiNotesLineEntriesId = NLE.pkiNotesLineEntriesId,
                         fkiAccountId = NLE.fkiAccountId,
                         NotesColumnName = NC.NotesColumnName,
                         Value = NLE.Value,
                         isNewLineItem = NLE.isNewEntry,
                         Sequence = NC.Sequence

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

        AccountsLineEntriesIList = query;

        return AccountsLineEntriesIList;
    }

我根本无法想到解决方案,我希望有人能引导我朝着正确的方向前进。

谢谢。

2 个答案:

答案 0 :(得分:0)

如果您有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);
}

然后使用标题数组将其传递给您的视图,并将它们分别渲染到表格中。

如果没有特定尺寸,您可以使用此

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

答案 1 :(得分:0)

感谢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; }
}

我的存储库中的函数(与包含我所有函数的类相同):

//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>

我知道这对我所需要的东西非常习惯,但如果他们正在考虑做同样的事情,也许这可以帮助其他人有想法。我仍然认为有更好的方法可以做到这一点,但我在这方面努力寻找更好的解决方案。

安德烈,再次感谢你带领我走向正确的方向。