public ActionResult Rating(int idPhoto) { var rating = new RatingEntity() { UserName = User.Identity.Name, PhotoId = idPhoto }; PhotoEntity photo = null; try { photo = _photoService.GetById(idPhoto); } catch { return(RedirectToAction("Error", "Error")); } var photoRating = new PhotoInformViewModel { PhotoId = photo.Id }; if (ReferenceEquals(photo.Ratings.FirstOrDefault(p => p.UserName == rating.UserName), null)) { _photoService.AddRating(rating); photoRating.IsSelected = true; } else { _photoService.DeleteRating(rating); photoRating.IsSelected = false; } try { photoRating.RatingsCount = _photoService.GetById(photo.Id).Ratings.Count; } catch { return(RedirectToAction("Error", "Error")); } return(Json(photoRating)); }