public override object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
        {
            ProductReviewOperation currentOperation;

            if (controllerContext.HttpContext.Request.Form["Approve"] != null)
            {
                currentOperation = ProductReviewOperation.Approve;
            }
            else if (controllerContext.HttpContext.Request.Form["Reject"] != null)
            {
                currentOperation = ProductReviewOperation.Reject;
            }
            else if (controllerContext.HttpContext.Request.Form["Delete"] != null)
            {
                currentOperation = ProductReviewOperation.Delete;
            }
            else
            {
                return new ReviewUpdateModel();
            }

            var nameValueCollection = controllerContext.HttpContext.Request.Form;

            var keys = nameValueCollection.AllKeys.Where(s => s.StartsWith("review-"));
            var reviewUpdateModel = new ReviewUpdateModel
            {
                CurrentOperation = currentOperation,
                Reviews = new List<ProductReview>()
            };
            foreach (var key in keys)
            {
                var substring = key.Substring(7);
                int id;
                if (Int32.TryParse(substring, out id) && nameValueCollection[key].Contains("true"))
                {
                    var review = Session.Get<ProductReview>(id);
                    if (review != null)
                        reviewUpdateModel.Reviews.Add(review);
                }

            }
            return reviewUpdateModel;
        }
Exemplo n.º 2
0
        public void BulkAction(ReviewUpdateModel model)
        {
            var currentOperation = model.CurrentOperation;

            switch (currentOperation)
            {
                case ProductReviewOperation.Approve:
                    _session.Transact(session =>
                    {
                        foreach (var item in model.Reviews)
                        {
                            item.Approved = true;
                            Update(item);
                        }
                    });
                    break;
                case ProductReviewOperation.Reject:
                    _session.Transact(session =>
                    {
                        foreach (var item in model.Reviews)
                        {
                            item.Approved = false;
                            Update(item);
                        }
                    });
                    break;
                case ProductReviewOperation.Delete:
                    _session.Transact(session =>
                    {
                        foreach (var item in model.Reviews)
                        {
                            Delete(item);
                        }
                    });
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }