2016-02-25 38 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

抱歉,我的回复延迟。这显然是正确的部分 - 谢谢 –