I have 2 tables , Debtors and Payments, and i need to get for every record in debtors get sum from Payments of pay_amount column, here is i tryied to get sum of all column , but it gives me an error
The model element passed to the dictionary has the type "System.Collections.Generic.List1 [System.String]", but for this dictionary a model element of the type "System.Collections.Generic.IEnumerable
1 [Sb.Models.Debtor ] "
public class Debtor
{
public int Id { get; set; }
[Display(Name = "Person")]
public int person_id { get; set; }
[ForeignKey("person_id")]
public Person person {get; set;}
[Display(Name = "amount")]
public double amount { get; set; }
[Display(Name = "Project")]
public int project_id { get; set; }
[ForeignKey("project_id")]
public Project project { get; set; }
[Display(Name = "actions")]
public string actions { get; set; }
[Display(Name = "note")]
public string note { get; set; }
public ICollection<Payment> Payments { get; set; }
public Debtor()
{
Payments = new List<Payment>();
}
}
public class Payment
{
public int Id { get; set; }
[Display(Name ="debtor")]
public int? Debtor_id { get; set; }
[ForeignKey("Debtor_id")]
public Debtor debtor { get; set; }
[Required]
[Display(Name ="pay_amount")]
public double Pay_amount { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd'/'MM'/'yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Date_of_amount")]
public DateTime Date { get; set; }
}
[HttpGet]
public ActionResult Index()
{
SelectList persons = new SelectList(db.Persons, "Id", "FirstName");
ViewBag.persons = persons;
SelectList projects = new SelectList(db.Project, "Id", "Name");
ViewBag.projects = projects;
return View(db.Debtors
.Include(p => p.person)
.Include(p => p.project)
.Include(p => p.Payments)
.Select( s => s.Payments.Sum( x => x.Pay_amount).ToString())
.ToList());
}
and View Code
@model IEnumerable<Sb.Models.Debtor>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table table-bordered table-striped">
<tr>
<th>
@Html.DisplayNameFor(model => model.person_id)
</th>
<th>
@Html.DisplayNameFor(model => model.amount)
</th>
<th>
@Html.DisplayNameFor(model => model.project_id)
</th>
<th>
@Html.DisplayNameFor(model => model.actions)
</th>
<th>
@Html.DisplayNameFor(model => model.note)
</th>
<th>Pay amount</th>
<th>Date</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.person.Firstname)
</td>
<td>
@Html.DisplayFor(modelItem => item.amount)
</td>
<td>
@Html.DisplayFor(modelItem => item.project.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.actions)
</td>
<td>
@Html.DisplayFor(modelItem => item.note)
</td>
<td>
@Html.DisplayFor(modelItem => item.Payments.FirstOrDefault().Pay_amount)
</td>
<td>
@Html.DisplayFor(modelItem => item.Payments.FirstOrDefault().Date)
</td>
<td>
@*@Html.DisplayFor(modelItem => item.)*@
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id }) |
@Html.ActionLink("Pay", "PaymentCreate", new { id = item.Id }, new { @class = "pay"})
</td>
</tr>
}
</table>
</script>