Reputation: 7314
pretty new to mvc 5.
I have a model that contains a field and a list.
I wish to display both in my view.
this is my model:
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; }
}
my controller:
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);
}
my view:
@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>
but errors here:
InformedProducts.Models.AdminChargeHistory
and here? @Html.LabelFor(m=>m.ErrorMessage)
I cannot see what I am doing wrong?
Upvotes: 0
Views: 737
Reputation: 13949
Model - Add getter/setter for Charges and empty constructor.
public class AdminChargeHistory
{
public AdminChargeHistory()
{
Charges = new List<StripeChargeHistory>();
}
public ICollection<StripeChargeHistory> Charges { get; set; }
public string ErrorMessage { get; set; }
}
Controller - take advantage of 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);
}
View - Reference the Charges collection property
@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>
if you have red squiggly under @model InformedProducts.Models.AdminChargeHistory
you might have the wrong namespace.. Remove InformedProducts.Models.
and put cursor on AdminChargeHistory
and hit ctrl + . (period) and it should find the correct namespace.
Upvotes: 1
Reputation: 2585
It should be:
@foreach (var item in Model.Charges)
{
// code removed for brevity...
}
This is because your model AdminChargeHistory
has a property named Charges
which is enumerable. What you are using right now: Model.StripeChargeHistory
won't work because there is no such property in the class AdminChargeHistory
.
Also, you have public string ErrorMessage { get; set;] }
, that bracket looks like a problem.
Upvotes: 1