Copied to clipboard

Flag this post as spam?

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


  • John Schuldt 6 posts 36 karma points
    Sep 11, 2013 @ 21:05
    John Schuldt
    0

    return JSON data from MVC controllers

    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!

  • Dan Diplo1603 karma points
    Sep 11, 2013 @ 21:08
    Dan Diplo
    0

    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 Schuldt36 karma points
    Sep 11, 2013 @ 21:15
    John Schuldt
    0

    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 Butland1322 karma points c-trib
    Sep 11, 2013 @ 22:06
    Andy Butland
    1

    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 Schuldt36 karma points
    Sep 12, 2013 @ 01:07
    John Schuldt
    0

    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 Butland1322 karma points c-trib
    Sep 12, 2013 @ 09:15
    Andy Butland
    0

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

  • John Schuldt36 karma points
    Sep 12, 2013 @ 18:01
    John Schuldt
    0

    You're the best!

     

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

  • Ibrahim Uslu24 karma points
    Oct 03, 2013 @ 21:30
    Ibrahim Uslu
    0

    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 Butland1322 karma points c-trib
    Oct 04, 2013 @ 09:14
    Andy Butland
    0

    Try umbraco/surface/StoreSurface/Checkout

Please Sign in or register to post replies

Write your reply to:

Draft
Our.umbraco.org is the community mothership for Umbraco, the open source asp.net cms. With a friendly forum for all your questions, a comprehensive documentation and a ton of packages from the community. This site is running Umbraco version 7.3.0-RC