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 =&GT; m.ErrorMessage)
我看不出我做错了什么?
答案 0 :(得分:1)
应该是:
@foreach (var item in Model.Charges)
{
// code removed for brevity...
}
这是因为您的模型AdminChargeHistory
具有可枚举的名为Charges
的属性。您现在使用的是:Model.StripeChargeHistory
无效,因为类AdminChargeHistory
中没有此类属性。
此外,您有public string ErrorMessage { get; set;] }
,该括号看起来像是一个问题。
答案 1 :(得分: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
@{
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>
如果你在@model InformedProducts.Models.AdminChargeHistory
下有红色波浪形,你可能有错误的命名空间。删除InformedProducts.Models.
并将光标放在AdminChargeHistory
上并点击ctrl +。 (句号),它应该找到正确的命名空间。