Hi,
I've been reading the documentation about surface controllers and the model builder etc and I decided to build a new site. I'm trying to get away from putting my logic in the view and instead move it to a custom controller.
Im having some issues getting my head around how to get started though. I've worked on sites that have this all setup already but starting from scratch is new to me.
I'm just using a fresh install of Umbraco with the starter kit, so have the following content.
All I am trying to do just now is make a homepage which shows the blog post titles on it. Nothing special but I can't get my head around how I do this.
The doctype is the controller name, from what I understand from the documentation, so since I'm on the home node, the controller would be HomeController.
I would need to hook that up to the Home Model but I would also need the blog model too. The home model would have the site name, seo stuff etc and the blog model would have all the info about the blog posts.
I guess it depends on what you are trying to do actually. If you are intercepting the full rendering of the page, I think you are more likely to use a Render controller than a Surface controller.
However, for the segment you are referring to, I would create a surface controller called (for example) LatestFeedsController. This would have an action on it called "Blogs", maybe with some parameters like how many to return. This action would then return a partial view that just renders out the results
Then in my Homepage view, I would call this action using @Html.Action()
You're correct about using HomeController as the controller name since your doctype alias is home.
Your model could look something like this:
public class HomeModel : RenderModel {
public HomeModel (IPublishedContent content) : base(content) { }
//custom properties
public string Title { get; set; }
}
Your controller could look like this:
public class HomeController : RenderMvcController {
public ActionResult Home(RenderModel model) {
//create a new instance to use on your view
HomeModel vm = new HomeModel();
//return the viewresult
return View("~/Views/Home.cshtml", vm);
}
}
Your view file (home.cshtml for example) could look like this:
Using MVC rather than all logic in view
Hi, I've been reading the documentation about surface controllers and the model builder etc and I decided to build a new site. I'm trying to get away from putting my logic in the view and instead move it to a custom controller.
Im having some issues getting my head around how to get started though. I've worked on sites that have this all setup already but starting from scratch is new to me.
I'm just using a fresh install of Umbraco with the starter kit, so have the following content.
All I am trying to do just now is make a homepage which shows the blog post titles on it. Nothing special but I can't get my head around how I do this.
The doctype is the controller name, from what I understand from the documentation, so since I'm on the home node, the controller would be HomeController. I would need to hook that up to the Home Model but I would also need the blog model too. The home model would have the site name, seo stuff etc and the blog model would have all the info about the blog posts.
How do I do this? Is this correct?
Hi Owain,
I guess it depends on what you are trying to do actually. If you are intercepting the full rendering of the page, I think you are more likely to use a Render controller than a Surface controller.
However, for the segment you are referring to, I would create a surface controller called (for example) LatestFeedsController. This would have an action on it called "Blogs", maybe with some parameters like how many to return. This action would then return a partial view that just renders out the results
Then in my Homepage view, I would call this action using @Html.Action()
Does that help?
Nik
Hi Owain,
You're correct about using HomeController as the controller name since your doctype alias is home.
Your model could look something like this:
Your controller could look like this:
Your view file (home.cshtml for example) could look like this:
For further information I suggest you'd take a look at the documentation; https://our.umbraco.org/documentation/reference/routing/custom-controllers
Of course! That makes sense to put it in a partial view and then pull that partial on to the homepage! Feel stupid now!
Thanks for the quick reply :)
is working on a reply...