Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • nickornotto 397 posts 900 karma points
    Oct 11, 2015 @ 10:31
    nickornotto
    0

    Controller not found when posting form via ajax

    I have a form which I'm trying to send via ajax to a custom controller.

    Form is rendered in a helper (in App_Code) and helper is called in one of my views.

    Controller is under Controllers folder.

    The problem is that I always get:

    POST http://localhost:49844/Booking/Book 404 (Not Found)
    

    so simply my app cannot find the controller and the action. btw it's not the problem of parameter as I removed parameters and just tried to call pure action and still couldn't find it.

    I even converted the form to BeginUmbracoForm and still couldn't find my custom controller

    Form:

    @helper Modal()
    {
        var wvp = PageContext.Page as System.Web.Mvc.WebViewPage;
    
        var Html = wvp.Html;
        var Ajax = wvp.Ajax;
    
            <form id="bookingForm" name="bookingForm" class="modal-form">
                <fieldset>
                    <label for="Name">Name</label>
                    <input type="text" name="Name" id="Name" />
                    <label for="Email">Email</label>
                    <input type="text" name="Email" id="Email"" />
    
                    <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
                </fieldset>
            </form>
    }
    

    Ajax:

            function book() {
                var url = "/Booking/Book";
                var data = JSON.stringify({ model: $('#bookingForm').serialize()});
                $.ajax({
                    contentType: 'json',
                    method: "POST",
                    url: url,
                    data: data,
                    success: alert("success"),
                    dataType: "html"
                });
            }
    

    Controller:

    public class BookingController : SurfaceController
    {
        [HttpPost]
        public ActionResult Book(BookingModel model)
        {
    
            return this.View();
        }
    }
    

    What am I missing?

  • Yasir Butt 161 posts 371 karma points
    Oct 11, 2015 @ 10:43
    Yasir Butt
    100

    Hi,

    As you have inherited your controller with SurfaceController so your url will be

    http://localhost:49844/Umbraco/Surface/Booking/Book/

    I hope this will help you!

    Yasir

  • nickornotto 397 posts 900 karma points
    Oct 11, 2015 @ 12:12
    nickornotto
    0

    I tried it already and it didn't work. Still doesn't work with /Umbraco/Surface

  • Yasir Butt 161 posts 371 karma points
    Oct 11, 2015 @ 14:53
    Yasir Butt
    0

    Can you please make a get method in your controller and see is it working with umbraco/surface?

    i am afraid that your json request is not sending the same parameter as expected.

  • nickornotto 397 posts 900 karma points
    Oct 12, 2015 @ 17:46
    nickornotto
    0

    Ok, I made it working. Thanks for hints!

Please Sign in or register to post replies

Write your reply to:

Draft