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 :)
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.
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.
@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?
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.
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?
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
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.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
@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?
No, you should only use
UmbracoApiController
for your ajax calls. Everything else goes either throughRenderMvcController
(route hijacking) if you just need to display data on a page or aSurfaceController
if you need to submit forms.You can hack
SurfaceController
s 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.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?
is working on a reply...