public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var context = new ActionContext
            {
                Parameters =
                    bindingContext.HttpContext.Request.Form.Keys
                    .ToDictionary(x => x, x => bindingContext.HttpContext.Request.Form[x].ToString()),
                CustomerCode = GetValue(CustomerCodeKey, bindingContext)
            };

            Guid.TryParse(GetValue(BackendSidKey, bindingContext), out var backendSid);

            context.BackendSid = backendSid;
            context.UserId     = _provider.GetUserId();
            context.UserName   = _provider.GetUserName();

            int.TryParse(GetValue(ContentIdKey, bindingContext), out var contentId);

            context.ContentId  = contentId;
            context.ActionCode = GetValue(ActionCodeKey, bindingContext);

            var contentItems   = GetValue(ContentItemIdKey, bindingContext);
            var contentItemIds = new List <int>();

            if (!string.IsNullOrEmpty(contentItems))
            {
                var ids = contentItems.Split(',');

                foreach (var id in ids)
                {
                    if (int.TryParse(id, out var contentItemId))
                    {
                        if (contentItemId <= 0)
                        {
                            bindingContext.ModelState.AddModelError(ContentItemIdKey, id + " must be greater than 0");
                            return(Task.CompletedTask);
                        }

                        contentItemIds.Add(contentItemId);
                    }
                    else
                    {
                        bindingContext.ModelState.AddModelError(ContentItemIdKey, id + " must be integer");
                        return(Task.CompletedTask);
                    }
                }
            }

            context.ContentItemIds = contentItemIds.ToArray();

            bindingContext.Result = ModelBindingResult.Success(context);
            return(Task.CompletedTask);
        }
Beispiel #2
0
        public ActionResult Publish(SendProductModel model)
        {
            var action = ObjectFactoryBase.Resolve <PublishAction>();

            if (model != null && ModelState.IsValid)
            {
                var idsList = model.ArticleIds.SplitString(' ', ',', ';', '\n', '\r').Distinct().ToArray();
                if (idsList.Length > 200)
                {
                    ModelState.AddModelError("", ProductCardStrings.TooMuchProducts);
                    return(View("Send", model));
                }

                int[] ids;
                try
                {
                    ids = idsList.Select(int.Parse).ToArray();
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ProductCardStrings.NotNumberValues + ". " + ex.Message);
                    return(View("Send", model));
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        var context = new ActionContext {
                            ContentId = 288, ContentItemIds = ids
                        };
                        action.Process(context);
                        model.Message = ProductCardStrings.PublishedAndSendSuccess;
                    }
                    catch (ActionException ex)
                    {
                        model.Message = ex.Message;
                    }
                }
            }

            return(View("Send", model));
        }