2017-03-23 44 views
-1

我有以下2個表:填充字段值作爲數據列查看MVC

此表包含列: enter image description here

此表包含與這些列中的值: enter image description here

我想在這種格式在我看來:

enter image description here

伊夫放入其中,我通過我的模型傳遞給視圖一個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; } 
} 

我使用下面的填充數據:

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

和IM填充它和傳球它是這樣的:

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

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

     NVM.BudgetNoteLineEntryList = _budgetEntryRepository.GetBudgetNoteLineEntry(accId); 

     return PartialView("_ShowAccountBudgetLineEntries", NVM); 
    } 

我根本無法想到了解決辦法,我希望有人可能會導致我在正確的方向。

謝謝。

+0

對不起,我應該寧願向我展示2張表格,以及我正在從中提取的數據。兩者之間的聯繫是fkiNotesColumnId - Id相信這是標識哪些值屬於哪個列的字段?希望這有助於多一點? – AxleWack

+1

@AxleWack你可以創建一個二維數組,其中所有數據將被排序,並將其與第一個表中的標題分開呈現。 'ViewModel'應該包含已經格式化的數據。 –

+0

我會研究它,並試試看。謝謝安德烈 – AxleWack

回答

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

它可能更多,也可能少於8,但順序應該總是按順序運行。我完全沒有想法,所以請分享更多信息,我會試着看看它。 – AxleWack

+0

@AxleWack我編輯了我的答案以匹配您的案例 –

+0

不幸的是,上述方法無法正常工作,因爲fkiNotesColumnId的排序可能有所不同,具體取決於列的順序。但是,您通過使用2D陣列確實讓我朝着正確的方向發展!所以我爲此感謝你!我會發布我的代碼供所有人查看。 – AxleWack

0

感謝來自安德烈的幫助下,我利用二維數組。我的方式做到了也許不是最好的出路在那裏,但它的工作:

視圖模型:在我的倉庫

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> 

我知道這是很習慣我需要什麼,但也許這可以幫助別人有想法,如果他們正在尋找在做同樣的事情。我仍然認爲有這樣做的更好的方法,但是爲了找到更好的解決方案,我一直苦苦掙扎。

安德烈,再次感謝您帶領我走向正確的方向。