Copied to clipboard

Flag this post as spam?

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


  • Owain Williams 481 posts 1413 karma points MVP 7x c-trib
    Dec 28, 2017 @ 11:14
    Owain Williams
    0

    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. enter image description here

    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?

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Dec 28, 2017 @ 11:29
    Nik
    1

    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

  • Tom 34 posts 154 karma points
    Dec 28, 2017 @ 11:29
    Tom
    100

    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:

    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:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<HomeModel>
    <h1>@Model.Title</h1>
    

    For further information I suggest you'd take a look at the documentation; https://our.umbraco.org/documentation/reference/routing/custom-controllers

  • Owain Williams 481 posts 1413 karma points MVP 7x c-trib
    Dec 28, 2017 @ 11:36
    Owain Williams
    0

    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 :)

Please Sign in or register to post replies

Write your reply to:

Draft