2016-02-25 73 views
0

對於mvc來說很新5.如何使用剃鬚刀顯示列表和字段

我有一個包含字段和列表的模型。

我希望在我看來都顯示。

這是我的模型:

public class AdminChargeHistory 
{ 
    public List<StripeChargeHistory> Charges = new List<StripeChargeHistory>(); 
    public string ErrorMessage { get; set;] } 
} 

public class StripeChargeHistory 
{ 
    public int TransactionId { get; set; } 
    public string Amount { get; set; } 
    public string AmountRefunded { get; set; } 
    public string CustomerId { get; set; } 
    public Nullable<bool> Paid { get; set; } 
    public string Status { get; set; } 
    public string ChargeId { get; set; } 
    public string InvoiceId { get; set; } 
    public string BalanceTransId { get; set; } 
    public Nullable<bool> Live { get; set; } 
    public Nullable<int> Month { get; set; } 
    public Nullable<int> Year { get; set; } 
} 

我的控制器:

public ActionResult MissedPayments() 
{ 
    var adminChargeHistory = new AdminChargeHistory(); 
    try 
    { 
     var stripeRepository = new StripeRepository();    
     var results = stripeRepository.GetMissedPayments(-1, -1, false); 
     foreach (var result in results) 
     { 
      var stripeChargeHistory = new StripeChargeHistory(); 
      stripeChargeHistory.Amount = result.Amount; 
      stripeChargeHistory.AmountRefunded = result.AmountRefunded; 
      stripeChargeHistory.BalanceTransId = result.BalanceTransId; 
      stripeChargeHistory.ChargeId = result.ChargeId; 
      stripeChargeHistory.CustomerId = result.CustomerId; 
      stripeChargeHistory.InvoiceId = result.InvoiceId; 
      stripeChargeHistory.Live = result.Live; 
      stripeChargeHistory.Month = result.Month; 
      stripeChargeHistory.Paid = result.Paid; 
      stripeChargeHistory.Status = result.Status; 
      stripeChargeHistory.TransactionId = result.TransactionId; 
      stripeChargeHistory.Year = result.Year; 
      adminChargeHistory.Charges.Add(stripeChargeHistory); 
     } 
    } 
    catch (Exception ex) 
    { 
     adminChargeHistory.ErrorMessage = ex.Message; 
    } 

    return View(adminChargeHistory); 
} 

我的觀點:這裏

@model InformedProducts.Models.AdminChargeHistory 
@{ 
    ViewBag.Title = "Missed Payments"; 
} 

<h2>Missed Payments</h2> 
<table class="grid"> 
    <tr> 
     <th>Customer Id</th> 
     <th>Amount</th> 
     <th>Month</th> 
     <th>Year</th> 
    </tr> 

    @foreach (var item in Model.StripeChargeHistory) 
    { 

     <tr> 
      <td class="left"><@item.CustomerId></td> 
      <td class="left"><@item.Amount></td> 
      <td class="left"><@item.Month></td> 
      <td class="left"><@item.Year></td> 
     </tr> 

    @} 
    @Html.LabelFor(m=>m.ErrorMessage) 
</table> 

但錯誤:

InformedProducts.Models.AdminChargeHistory 

和這裏? @ Html.LabelFor(m => m.ErrorMessage)

我看不到我做錯了什麼?

+0

'「但這裏的錯誤」' - 錯誤是......? – David

+0

剛纔說的在設計視圖中找不到 –

+0

對於初學者來說,在你的'AdminChargeHistory'類中有一個輸入錯誤會阻止它編譯。所以你至少在編譯之前不能使用它。編譯器當然會告訴你關於*那個錯誤?至於「未找到」,你能否展示它的屏幕截圖?通常有更具描述性的東西。 – David

回答

1

模型 - 爲Charges和空構造函數添加getter/setter。

public class AdminChargeHistory 
{ 
    public AdminChargeHistory() 
    { 
     Charges = new List<StripeChargeHistory>(); 
    } 

    public ICollection<StripeChargeHistory> Charges { get; set; } 
    public string ErrorMessage { get; set; } 
} 

控制器 - 利用LINQ

public ActionResult MissedPayments() 
{ 
    var adminChargeHistory = new AdminChargeHistory(); 
    try 
    { 
     var stripeRepository = new StripeRepository(); 
     var results = stripeRepository.GetMissedPayments(-1, -1, false); 

     adminChargeHistory.Charges = results.Select(result => new StripeChargeHistory 
     { 
      Amount = result.Amount, 
      CustomerId = result.CustomerId, 
      Month = result.Month, 
      Year = result.Year 
     }).ToList(); 
    } 
    catch (Exception ex) 
    { 
     adminChargeHistory.ErrorMessage = ex.Message; 
    } 

    return View(adminChargeHistory); 
} 

視圖 - 如果你有紅色波浪下@model InformedProducts.Models.AdminChargeHistory你可能有錯誤的命名空間引用的收費集合屬性

@model InformedProducts.Models.AdminChargeHistory 
@{ 
    ViewBag.Title = "Missed Payments"; 
} 

<h2>Missed Payments</h2> 
<table class="grid"> 
    <tr> 
     <th>Customer Id</th> 
     <th>Amount</th> 
     <th>Month</th> 
     <th>Year</th> 
    </tr> 

    @foreach (var item in Model.Charges) 
    { 

     <tr> 
      <td class="left"><@item.CustomerId></td> 
      <td class="left"><@item.Amount></td> 
      <td class="left"><@item.Month></td> 
      <td class="left"><@item.Year></td> 
     </tr> 

    @} 
    @Html.LabelFor(m=>m.ErrorMessage) 
</table> 

..刪除InformedProducts.Models.並將光標放在AdminChargeHistory上並按下ctrl +。 (句點),它應該找到正確的名稱空間。

+0

偉大的答案 - 我一直忘記使用LINQ那樣的。雖然我正確地假設我不需要構造函數>?由於我沒有包括一個,它工作正常 –

+0

@AndrewSimpson在這種情況下,你可能不需要它。我喜歡這樣做,以避免將來出現空引用錯誤 – JamieD77

1

它應該是:

@foreach (var item in Model.Charges) 
{ 
    // code removed for brevity... 
} 

這是因爲你的模型AdminChargeHistory有一個名爲Charges屬性,該屬性可枚舉。您現在正在使用的是:Model.StripeChargeHistory將不起作用,因爲類AdminChargeHistory中沒有此類屬性。

此外,你有public string ErrorMessage { get; set;] },該支架看起來像一個問題。

+0

我相信你的權利。遠離個人電腦,但會盡快回應。感謝您的回答 –

+0

抱歉,我的回覆延遲。這顯然是正確的部分 - 謝謝 –