Reputation: 13
What I want to do is get values from my payments table and make a sum
But in my return I get this issue in return View(model); :
Argument type models.Payments is not assignable to model type 'System.Collections.Generic.IEnumerable'
I get this error because I don't assign correct model in my View
Upvotes: 0
Views: 277
Reputation: 14133
Your problem is that you are sending a model of a different type, so your view is specting a list of EarningsViewModel
:
@model IEnumerable<MyApp.Models.ViewModels.EarningsViewModel>
And you are passing a Payments
object.
Upvotes: 0
Reputation: 218732
Your razor view is strongly typed to a collection of EarningsViewModel. But from your action method, you are passing a single object of Payments
class. So you are getting this error !
Ideally you should create a new view model for this specific view.
public class ClientPaymentInfo
{
public string ClientName { set;get;}
public List<PaymentInfoVm> Payments { set;get;}
public ClientPaymentInfo()
{
Payments = new List<PaymentInfoVm>();
}
}
public class PaymentInfoVm
{
public int Id { set;get;}
public string PaymentNumber { set;get;}
public decimal Amount { set;get;}
}
Now you should update your razor view to be strongly typed to that type.
@model ClientPaymentInfo
<h2>Payments of @Model.ClientName</h2>
@foreach(var item in Model.Payments)
{
<p>@item.Amount</p>
<p>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</p>
}
<p>Total : @Model.Payments.Sum(s=>S.Amount)
So your action method will be
public ActionResult Index(int id)
{
var vm= new ClientPaymentInfo { ClientName = "Hard coded Client Name"};
vm.Payments = db.PaymentsList.Where(x => x.ClientsId == id)
.Select(d=>new PaymentInfoVm {
Id=d.Id,
Amount=d.Amount,
PaymentNumber=d.Paymentnumber}).ToList();
return View(vm);
}
Upvotes: 1