/// <summary> /// Create a new BookVote object. /// </summary> /// <param name="voteID">Initial value of the VoteID property.</param> /// <param name="bookID">Initial value of the BookID property.</param> /// <param name="addedByIP">Initial value of the AddedByIP property.</param> /// <param name="addedDate">Initial value of the AddedDate property.</param> /// <param name="addedVote">Initial value of the AddedVote property.</param> public static BookVote CreateBookVote(global::System.Int32 voteID, global::System.Int32 bookID, global::System.String addedByIP, global::System.DateTime addedDate, global::System.Int32 addedVote) { BookVote bookVote = new BookVote(); bookVote.VoteID = voteID; bookVote.BookID = bookID; bookVote.AddedByIP = addedByIP; bookVote.AddedDate = addedDate; bookVote.AddedVote = addedVote; return bookVote; }
/// <summary> /// Deprecated Method for adding a new object to the BookVotes EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToBookVotes(BookVote bookVote) { base.AddObject("BookVotes", bookVote); }
public JsonResult Vote(string widget_id, string clicked_on) { int id = 0; int.TryParse(widget_id, out id); Book book = db.Books.FirstOrDefault(b => b.BookID == id); if (book != null) { var selectedMark = Request.Cookies["rate_" + widget_id]; if (selectedMark != null) { return Json(null); } string starNumber = clicked_on.Substring(5, 1); // clicked_on содержит строку типа "star_3 ..." - нам нужна цифра, т.е. 5-й символ в строке int rate; bool canParse = int.TryParse(starNumber, out rate); if (canParse) { book.Votes++; book.TotalRating += rate; // Сохраняем оценку в лог-таблицу BookVotes (в каждой таблице должен быть Primary Key!!!) BookVote vote = new BookVote { BookID = book.BookID, AddedVote = rate, AddedBy = User.Identity.Name, AddedByIP = Request.UserHostAddress, AddedDate = DateTime.Now }; db.BookVotes.AddObject(vote); db.SaveChanges(); var result = new { mark = starNumber, widget_id = widget_id, number_votes = book.Votes, total_points = book.TotalRating, dec_avg = book.RatingDecAvg.ToString(), // обязательно преобразовываем в строку, иначе во вьюхе получим '3.7' вместо '3,7' whole_avg = book.RatingIntAvg }; return Json(result); } } return Json(null); }