コード例 #1
0
        public ActionResult Submit()
        {

            // Variables.
            var tempGuid = default(Guid);
            var keys = Request.Form.AllKeys;
            var formId = Guid.Parse(Request["FormId"]);


            // Get values.
            var values = keys
                .Where(x => Guid.TryParse(x, out tempGuid))
                .Select(x => new FieldSubmission
                {
                    FieldId = Guid.Parse(x),
                    FieldValues = Request.Form.GetValues(x)
                })
                .ToList();


            // Submit form.
            var result = Submissions.SubmitForm(formId, values, new SubmissionOptions()
            {
                Validate = Config.EnableServerSideValidation
            });


            // Return result.
            return Json(new
            {
                Success = result.Success
            });

        }
コード例 #2
0
        public ActionResult Submit()
        {
            // Variables.
            var tempGuid = default(Guid);
            var keys     = Request.Form.AllKeys;
            var fileKeys = Request.Files.AllKeys;
            var formId   = Guid.Parse(Request["FormId"]);
            var pageId   = NumberUtility.AttemptParseInt(Request["PageId"]);
            var pageNode = pageId.HasValue
                ? Umbraco.TypedContent(pageId.Value)
                : null;
            var pageUrl  = pageNode?.Url;
            var pageName = pageNode?.Name;


            // Get values.
            var values = keys
                         .Where(x => Guid.TryParse(x, out tempGuid))
                         .Select(x =>
            {
                var fieldValue = Request.Form.GetValues(x);
                return(new FieldSubmission
                {
                    FieldId = Guid.Parse(x),
                    FieldValues = fieldValue
                });
            })
                         .ToList();

            // Get file values.
            var fileValues = fileKeys
                             .Where(x => Guid.TryParse(x, out tempGuid))
                             .Select(x =>
            {
                // Variables.
                var fileValue = Request.Files.Get(x);


                // Extract file data: http://stackoverflow.com/a/16030326/2052963
                var reader   = new BinaryReader(fileValue.InputStream);
                var fileData = reader.ReadBytes((int)fileValue.InputStream.Length);
                var filename = fileValue.FileName;


                // Return file field submission.
                return(new FileFieldSubmission()
                {
                    FieldId = Guid.Parse(x),
                    FileData = fileData,
                    FileName = filename
                });
            })
                             .ToList();


            // Payload.
            var payload = new[]
            {
                new PayloadSubmission()
                {
                    Name  = "URL",
                    Value = pageUrl
                },
                new PayloadSubmission()
                {
                    Name  = "Page Name",
                    Value = pageName
                }
            }.Where(x => !string.IsNullOrWhiteSpace(x.Value));


            // Submit form.
            var context = new FormRequestContext()
            {
                CurrentPage    = pageNode,
                HttpContext    = HttpContext,
                Services       = Services,
                UmbracoContext = UmbracoContext,
                UmbracoHelper  = Umbraco
            };
            var options = new SubmissionOptions()
            {
                Validate = Config.EnableServerSideValidation
            };
            var result = Submissions.SubmitForm(formId, values, fileValues, payload, options, context);


            // Return result.
            return(Json(new
            {
                Success = result.Success
            }));
        }