Copied to clipboard

Flag this post as spam?

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


  • Peter Hansen 29 posts 72 karma points
    Feb 13, 2018 @ 16:28
    Peter Hansen
    0

    Controllers and what to do with them

    Hi there, I am (again) pretty new to Umbraco and I started creating a site. I am pretty much lost when it comes to the different types of controllers. I've created a separate project for my controllers only (which I btw write in VB as I am a VB'er). The connection between the solution and the controller project works But I think I am doing it wrong when it comes to controller usage. Maybe you guys can help. I need a controller with a constructor that loads before the view loads (to fill up some ViewDatas). I also need a controller with some functions I call using ajax. So for instance if I have a view called Cart I would create a controller called CartController. That controller class inherits from umbraco.Web.Mvc.RenderMvcController and then it's possible to create the Cart-function that will run before the view is loaded (sort of a constructor). If I inherits from SurfaceController I don't get this constructor-like functionality - this makes sense because the RenderMvcController is a render controller. I get that. Instead I get to be able to call functions using ajax. At the same time - using the RenderMvcController I am not able to call functions from ajax hence that sort of functionality belongs to the SurfaceController.

    But I would really like a mix of the render controller and the surfacecontroller where I get the opportunity to put stuff into ViewData before the view is displayed AND to be able to execute functions in the same controller via ajax.

    This might be a weird question - please bear with me - I am pretty new to this world :)

    // peter

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 13, 2018 @ 16:55
    Sebastiaan Janssen
    1

    Hi Peter,

    What you want for your ajax calls is an UmbracoApiController - they only deal with XML/json data.

    For anything where you need to render stuff you'll want to do what you're currently doing anyway. Most of the time you don't really need to have a RenderMvcController though but that's up to you.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Feb 13, 2018 @ 17:01
    Dan Diplo
    1

    Rule of thumb:

    RenderMvcControllers are for returning a Model to your view. They are often used when you want to return a more complex strongly-typed view model and not just generic IPublishedContent.

    SurfaceControllers are for processing data that you (usually) POST to them. A classic example is a form submission, but you can use them in AJAX requests, too.

    You also have UmbracoApiControllers, which are usually used for returning JSON and often consumed from JavaScript.

    You can even use vanilla MVC controllers, but these don't automatically get access to the UmbracoContext and ApplicationContext, so are only useful for non-Umbraco content.

    You can use a SurfaceController for AJAX requests. Basically, use JS to post some data to your controller and return a PartialView from the controller that contains a fragment of HTML that you can then add to the DOM.

    Note sure if that helps, but

  • Peter Hansen 29 posts 72 karma points
    Feb 13, 2018 @ 22:19
    Peter Hansen
    0

    @Sebastian & Dan Yeah! Seems like it's the UmbracoAPIController I should be using. But I still need to add ViewData before the partial is completely loaded (so that I can load the ViewData in my partial). Does UmbracoApiController serve me some sort of a controller from where I can initialize these ViewData and then continue the view-load to fetch these ViewData?

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 13, 2018 @ 22:39
    Sebastiaan Janssen
    0

    No, you should only use UmbracoApiController for your ajax calls. Everything else goes either through RenderMvcController (route hijacking) if you just need to display data on a page or a SurfaceController if you need to submit forms.

    You can hack SurfaceControllers to do ajax-y things but you shouldn't, that's not what it's meant for. It's meant for doing form submissions.

    I am not sure what you need to pass ViewData around for but in my world it is rarely used. Rather, I'd recommend if you need to pass around data, then add it to a custom model that you return to a view.

  • Peter Hansen 29 posts 72 karma points
    Feb 17, 2018 @ 10:15
    Peter Hansen
    0

    I am not sure what you need to pass ViewData around for but in my world it is rarely used. Rather, I'd recommend if you need to pass around data, then add it to a custom model that you return to a view.

    In old days you would use ViewData to pass around data. I guess that's kind of old school and today you would probably want to use a model that includes a given set of properties. So my next question is: how do I send a model to my view before the view is loaded so that the properties (from the model) can be displayed on the view?

Please Sign in or register to post replies

Write your reply to:

Draft