Wednesday 17 August 2016

Form submit and its post code




 <form id = "main-contact-form" class="contact-form" name="contact-form" method="post" action="">
                    <div class="col-sm-10">
                        @*<div class="form-group">
                            <label>Name*</label>
                            <input type="text" name="name" id="name" class="form-control" required="required">
                        </div>*@
                        <div class="form-group">
                            <label>Subject*</label>
                            <input type = "text" name="subject" id="subject" class="form-control" required="required">
                        </div>
                        <div class="form-group">
                            <label>Message*</label>
                            <textarea name = "message" id="message" required="required" class="form-control" rows="8"></textarea>
                        </div>
                        <div class="form-group">
                            @*<a class="btn btn-primary btn-lg" onclick="Save()">Submit</a>*@
                           <a href="javascript:void(0);"> <button type="submit" id="Form" name="submit" class="btn btn-primary btn-lg">Submit Message</button></a>
                        </div>
                    </div>
                </form>


<script>
    $(document).ready(function () {
        $("#Form").on('click', function (e) {
            debugger;
            var subject = $("#subject").val();
            var message = $("#message").val();
            var id = @Model.Content.Id;
            $.ajax({
                url: "/umbraco/surface/BlogSurface/HandleCommentPost",
                type:"Get",
                dataType: "json",
                data: {'subject': subject, 'message': message,'id':id},
                cache: false,
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    if (result.success) {
                        alert(result.message);
                    }
                    alert(result.message);
                    window.location.reload();
                },
                failure: function(result) {
                    alert(result.message);
                    window.location.reload();
                }

            });
        });

    });
</script>

Surface Controller

namespace UmbLearning.controllers.SurfaceControllers
{
    // I don't encourage you to put your viewmodel class in your surface controller, only done for this example for readability
    public class BlogPostViewModel
    {
        [Required]
        public string Subject { get; set; }

        [Required]
        public string Message { get; set; }
    }

    public class BlogSurfaceController : SurfaceController
    {
        [HttpGet]
        public ActionResult RenderComments()
        {
            //Connect to the Umbraco DB
            var db = ApplicationContext.DatabaseContext.Database;

            //Get the current Umbraco Node ID
            var currentNodeId = UmbracoContext.PageId.Value;

            //Get an IENumberable of BlogComment objects to iterate over
            var comments = db.Query<BlogPostViewModel>("SELECT * FROM BlogComments WHERE BlogPostUmbracoId=@0", currentNodeId);

            //Return the view with our model and comments
            return PartialView("viewComments", comments);
        }

        [HttpGet]
        public ActionResult HandleCommentPost(string subject, string message,int id)
        {
            //Check if the data on the model is valid
            if (!ModelState.IsValid)
            {
                //There was a validation error with the data
                return CurrentUmbracoPage();
            }

            //Continue processing the data...

            //Create new Blog Post object
            var blogPostToAdd = new BlogComment();

            //Set values from view model & grab the current node ID
            blogPostToAdd.BlogPostUmbracoId = id;
            blogPostToAdd.Subject = subject;      
            blogPostToAdd.Message = message;

            //Get the Umbraco db
            var db = ApplicationContext.DatabaseContext.Database;

            //Add the object to the DB
            db.Insert(blogPostToAdd);

            //All done - redirect to the page
            return RedirectToCurrentUmbracoPage();
        }
    }
}

No comments:

Post a Comment