Copied to clipboard

Flag this post as spam?

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


  • budi erwanto 11 posts 61 karma points
    Oct 30, 2013 @ 05:30
    budi erwanto
    0

    Simple Example of SurfaceController ?

    Umbraco version 6.1.6

    Is there anyone can help me how to user surfacecontroller the simplest way, from model to controller and the view ?

    This is what i had done :

    - Create Model with name FeatureBlock.cs ( some properties e.g title, desc, media)

    - Create Controller FeatureBlockController without suffix "SurfaceController"

    This controller inherit from SurfaceController

    the index is :

     public class FeatureBlockController : Umbraco.Web.Mvc.SurfaceController

        {

            public ActionResult Index(RenderModel model)

            {

                return Content("asd");

            }

        }

     

    I already spent a day tinkering about this and still no luck. But when i use Umbraco.Web.Mvc.RenderMVCController the index controller is hit.

    1. So give me explantion when to user SurfaceController and when to use RenderMVCController ?

    2. Help me with the simple example Hello World from index action.

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Oct 30, 2013 @ 08:41
    Sebastiaan Janssen
    100

    Controller:

    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using My.Site.Models;
    
    
    namespace My.Site.Controllers
    { 
        public class FeatureBlockController : SurfaceController
        {       
            [ChildActionOnly]
            public ActionResult RenderFeatureBlock()
            {
                var featureBlock = new FeatureBlockModel { Desc = "Welcome", Media = "/media/test.jpg", Title = "Hello" };
                return PartialView("FeatureBlock", featureBlock);
            }
        }
    }
    

    Model:

    namespace My.Site.Models
    { 
        public class FeatureBlockModel
        {
            public string Title {get; set;}
            public string Desc {get; set;}
            public string Media {get; set;}
        }
    }
    

    View (~/Views/FeatureBlock/FeatureBlock.cshtml):

    @using My.Site.Models
    @model FeatureBlockModel
    
    <h1>@Model.Title</h1>
    @Model.Media<br />
    <p>@Model.Desc</p>
    

    In you template, call the following:

    @Html.Action("RenderFeatureBlock", "FeatureBlock")
    

    Note that this is almost exactly what you would do in "normal" MVC so if you need more info make sure to also look at resources about simple ASP.NET MVC.

    Hope this helps!

  • budi erwanto 11 posts 61 karma points
    Oct 30, 2013 @ 09:49
    budi erwanto
    0

    Thanks Sebastiaan.

     

    So basically this code is work, many thanks before. From what i see when using surface controller we must use [ChildActionOnly] tag and create the partial view ?

    Will be read again the documentaion. Thanks again

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Oct 30, 2013 @ 09:58
    Sebastiaan Janssen
    0

    Ah yes, I left that in there as best practice, you don't HAVE to use it, but it makes sense, read more about ChildActionOnly here: http://stackoverflow.com/questions/10253769/using-childactiononly-in-mvc

  • Comment author was deleted

    Oct 30, 2013 @ 10:16

    There's also a chapter on umbraco.tv about surface controllers http://umbraco.tv/videos/developer/fundamentals/surface-controllers/introduction/ :) teaches you the basics (including client and server validation) + you can download the completed example

  • budi erwanto 11 posts 61 karma points
    Oct 30, 2013 @ 14:39
    budi erwanto
    0

    Thanks Tim and sebastiaan will check that out. :D.

     

  • Willem Luijk 24 posts 95 karma points
    Aug 27, 2015 @ 05:38
    Willem Luijk
    0

    Sebastiaan,

    Suffix the surfacecontroller with SurfaceController is no longer needed?

    https://our.umbraco.org/documentation/reference/routing/surface-controllers ?

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Aug 27, 2015 @ 06:49
    Sebastiaan Janssen
    0

    It's not, which will be included in the next documentation update.

Please Sign in or register to post replies

Write your reply to:

Draft