示例#1
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var solution = Mapper.Map <Solution>(Model);

            solution.AuthorId = this.userManager.GetUserId(User);


            await this.solutionService.AddAsync(solution);

            if (Model.Attachments != null)
            {
                string path = await fileUploader.CreateAttachmentAsync(Model.Title, Model.Attachments, "Solutions");

                ICollection <SolutionAttachment> attachments = new List <SolutionAttachment>();

                foreach (var attachment in Model.Attachments)
                {
                    SolutionAttachment solutionAttachment = new SolutionAttachment
                    {
                        FileName   = attachment.FileName,
                        PathToFile = Path.Combine(path, attachment.FileName),
                        SolutionId = solution.Id
                    };
                    attachments.Add(solutionAttachment);
                }

                await this.attachmentService.AddRangeAsync(attachments);
            }

            await this.solutionService.SaveChangesAsync();

            alerter.AddMessage(MessageType.Success, "Solution created successfully");

            return(Redirect($"/Solutions/Details/{solution.Id}"));
        }
        public async Task <IActionResult> Create(RequestCreationBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var request = Mapper.Map <Request>(model);

            request.RequesterId = this.userManager.GetUserId(User);

            await this.requestService.AddAsync(request);

            if (model.Attachments != null)
            {
                string path = await fileUploader.CreateAttachmentAsync(model.Subject, model.Attachments, "Requests");

                ICollection <RequestAttachment> attachments = new List <RequestAttachment>();

                foreach (var attachment in model.Attachments)
                {
                    RequestAttachment requestAttachment = new RequestAttachment
                    {
                        FileName   = attachment.FileName,
                        PathToFile = Path.Combine(path, attachment.FileName),
                        RequestId  = request.Id
                    };
                    attachments.Add(requestAttachment);
                }

                await this.attachmentService.AddRangeAsync(attachments);
            }

            await this.requestService.SaveChangesAsync();

            this.alerter.AddMessage(MessageType.Success, "Request created successfully");

            return(this.RedirectToAction("Details", new { id = request.Id.ToString() }));
        }