Copied to clipboard

Flag this post as spam?

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


  • Saied 349 posts 674 karma points
    Jul 08, 2015 @ 20:24
    Saied
    0

    ApiController or SurfaceController for a Search Form?

    Let's assume I have a page with a form that has the ability to search cars. The user can enter the year, make, model, and engine and then click search.

    Search will go to a database and return a list of vehicles that matches what the user selected.

    I am a bit confused on if I should use an ApiController here or a SurfaceController here?

  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Jul 08, 2015 @ 22:24
    Matt Barlow | jacker.io
    0

    Assuming you want to update your search results asynchronously I would use the SurfaceController and corresponding view/model to render the form to take advantage of Validation and all that good stuff.

    The form would then be hooked up to post to an API Controller, I would pass the model via an AJAX request to the search method.

    There is some info about passing multiple parameters / models to Web API here: http://techbrij.com/pass-parameters-aspdotnet-webapi-jquery

    Like this:

    $.ajax({
     url: '/umbraco/api/cars/search',
     type: 'POST',
     data: { year: 2012, Make: 'Audi', Model: 'A3' },
     dataType: 'json',
     success: function (data) {
                       ...
      }
    }); 
    

    The search method consumes the data passed to it and does the search, returning the results as JSON.

    The JSON.parse() the data into objects, then use model binding to display the templated results with a JS library such as (handlebars JS).

    The good thing about using WebApi to expose your search methods is that you can call the same API methods from another form on your site quite easily.

Please Sign in or register to post replies

Write your reply to:

Draft