x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar6posts36karma

    return JSON data from MVC controllers

    John Schuldt started this topic September 11, 2013 @ 09:05

    Trying to Extend Umbraco 4.11.10

    Hi I have an MVC application that use's mvc controller actions to render and post json data to the server. I am having a difficult time extending the umbraco frontend to allow for the Ajax calls.

    How can I extend the SurfaceControllers to actually return JSON data?

    How do you set up the controller action URLs in umbraco?

    thanks!


  • Avatar610posts1012karma
    Comment with ID: 160783
    Dan Diplo posted this reply September 11, 2013 @ 09:08

    Can you upgrade to Umbraco 6.1 and then you can use the Web API controllers designed for this? See http://our.umbraco.org/documentation/Reference/WebApi/


  • John Schuldt posted this reply September 11, 2013 @ 09:15

    We haven't launched, but we are stuck at 4.11.10 at the moment.. Any ideas on a workaround that just uses the umbraco SurfaceController extension class?


  • Andy Butland posted this reply September 11, 2013 @ 10:06

    Hi John

    I'm on 6. something, but using the MVC controller to return Json data.

    So I'd expect this to work as a method in your surface controller:

    public JsonResult GetJsonData()
    {
        return Json(new { Message = "Hello world" }, JsonRequestBehavior.AllowGet);
    }
    

    It's then routed via: /umbraco/surface/MyController/GetJsonData

    Hope that helps

    Andy


  • John Schuldt posted this reply September 12, 2013 @ 01:07

    Thanks for the reply,

        [PluginController("DataSearch")]
        public class DataSearchSurfaceController : Umbraco.Web.Mvc.SurfaceController
        {      
            private DataFilterHelper dal = new DataFilterHelper();
            #region categories drop downs

            [HttpGet]
            public JsonResult GetCategories()
            {
                IEnumerable<CategoryModel> categ = dal.FindAllCategories();
                return Json(categ, JsonRequestBehavior.AllowGet);
            }
            #endregion

    }

    this wasn't routed to /umbraco/surface//GetCategories

    thanks!

     


  • Andy Butland posted this reply September 12, 2013 @ 09:15

    Just edited my post above - part of the URL I posted got eaten.


  • John Schuldt posted this reply September 12, 2013 @ 06:01

    You're the best!

     

    umbraco/surface/(controller class name w/o Controller)/GetJsonData gets me that data now


  • Ibrahim Uslu posted this reply October 3, 2013 @ 09:30

    Hi,

    I have a Controller, which har JsonResult method. I want to call this Json method from controller in my View. but it can not find the umbraco controller method. this works in pure MVC application.

     public class StoreSurfaceController: SurfaceController
        {
            [HttpPost]
            public JsonResult Checkout(ShoppingCartModel model)
            {
                string message = string.Format("Successfully processed {0} item(s).", model.CartItems.Count.ToString());
                return Json(new { Success = true, Message = message });
            }
        }

    Json to call controller from the view

      function PostData() {
            $.ajax({
                url: 'umbraco/surface/Store/Checkout',  //this is the umbraco surface path to controller action. problem her!!!!
                async: false,
                type: "POST",
                data: JSON.stringify(m_ShoppingCart),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR + "-" + textStatus + "-" + errorThrown);
                },
                success: function (data, textStatus, jqXHR) {
                    $("#Results").show();
                    $("#ResultMessage").html(data.Message);
                }
            });
        } 

     thanks in advance...


  • Andy Butland posted this reply October 4, 2013 @ 09:14

    Try umbraco/surface/StoreSurface/Checkout


Please login or Sign up To post replies