System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.Int32'.'

Joined
Feb 18, 2022
Messages
1
Reaction score
0
So i cant solve this problem im getting... Im creating a simple forum discussion offline and this error apears to me in this line :


"var Comments = _context.ArticlesComments.Where(d => d.ArticlesId.Equals(id.Value)).ToList();"


public IActionResult Details(int? id)
{
if (id == null)
{
return BadRequest();
}
Article article = _context.Articles.Find(id);
ArticleCommentViewModel vm = new ArticleCommentViewModel();

if (article == null)
{
return NotFound();
}
vm.ArticlesId = id.Value;
vm.Title = article.Title;
var Comments = _context.ArticlesComments.Where(d => d.ArticlesId.Equals(id.Value)).ToList();
vm.ListOfComments = Comments;

var ratings = _context.ArticlesComments.Where(d => d.ArticlesId.Equals(id.Value)).ToList();
if (ratings.Count() > 0)
{
var ratingSum = ratings.Sum(d => d.Rating);
ViewBag.RatingSum = ratingSum;
var ratingCount = ratings.Count();
ViewBag.RatingCount = ratingCount;
}
else
{
ViewBag.RatingSum = 0;
ViewBag.RatingCount = 0;
}

return View(vm);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top