public ActionResult Create() { string returnUrl = HttpContext.Request.Params["returnUrl"].Length > 0 ? HttpContext.Request.Params["returnUrl"] : HttpContext.Request.UrlReferrer.ToString(); try { Guid userGuid = new Guid(UserHelper.getLoggedInUserId()); Entities dbContext = new Entities(); int objectId; if (!int.TryParse(HttpContext.Request.Params["objectId"], out objectId)) { throw new Exception("Invalid object id specified."); } Models.Object obj = dbContext.Objects.SingleOrDefault(o => o.Id == objectId); if (obj == null) { throw new Exception("The object you are trying to comment on does not exist."); } if (HttpContext.Request.Params["content"].Length < 1) { throw new Exception("You must enter a comment."); } Comment comment = new Comment(); comment.UserId = userGuid; comment.ObjectId = obj.Id; comment.Content = HttpContext.Request.Params["content"]; comment.CreatedAt = DateTime.Now; dbContext.Comments.AddObject(comment); dbContext.SaveChanges(); return Redirect(returnUrl); }catch(Exception ex) { return Content(ex.Message); } }
/// <summary> /// Create a new Comment object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="userId">Initial value of the UserId property.</param> /// <param name="objectId">Initial value of the ObjectId property.</param> /// <param name="createdAt">Initial value of the CreatedAt property.</param> public static Comment CreateComment(global::System.Int32 id, global::System.Guid userId, global::System.Int32 objectId, global::System.DateTime createdAt) { Comment comment = new Comment(); comment.Id = id; comment.UserId = userId; comment.ObjectId = objectId; comment.CreatedAt = createdAt; return comment; }
/// <summary> /// Deprecated Method for adding a new object to the Comments EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToComments(Comment comment) { base.AddObject("Comments", comment); }