Reputation: 295
I call a model in view.cshtml like this:
@model IEnumerable<Spiirit_Project.Models.Deliverable>
Then I use this LINQ-method in view.cshtml so:
@{ var a = Model.Where(p => p.deliverable_research_year_id == 169).Count(); }
When I run my project, there is error: "Value cannot be null. Parameter name: source".
my "Deliverable" Table is not null but I get this error. How to fix this error?
there is my controller:
public ActionResult CreateDeliverable(int? idResearch, int? idBaseline, int? idYear){
ViewBag.IdResearch = idResearch;
ViewBag.IdBaseline = idBaseline;
ViewBag.IdYear = idYear;
ViewBag.Year = db.Deliverable_Research_Years.Find(idYear);
ViewBag.Deliverable = db.Deliverables.ToList();
return View();
}
Upvotes: 1
Views: 1041
Reputation: 295
@mjwills tell me in comment to pass in the model to the view. So I change my controller like this:
public ActionResult CreateDeliverable(int? idResearch, int? idBaseline, int? idYear){
ViewBag.IdResearch = idResearch;
ViewBag.IdBaseline = idBaseline;
ViewBag.IdYear = idYear;
ViewBag.Year = db.Deliverable_Research_Years.Find(idYear);
return View(db.Deliverables.ToList());
}
This is work correctly!
Upvotes: 3
Reputation: 35477
Try
@{ var a = Model?.Where(p => p.deliverable_research_year_id == 169).Count() ?? 0; }
This is assuming that deliverable_research_year_id
is NOT a nullable type.
Upvotes: -2