Copied to clipboard

Flag this post as spam?

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


  • Kristian Kjær 6 posts 56 karma points
    Apr 03, 2013 @ 17:57
    Kristian Kjær
    0

    Strongly-Typed Partial View Macro in 6.xx?

    Hello!

     

    I have a small setup with;

    • Umbraco 6.0.3
    • MVC4
    I'm trying to figure out how to code a strongly-typed partial view (the new macro partial). Somewhere down the road, I would like to have something like this available:
    @inherits Umbraco.Web.Macros.PartialViewMacroPage<FooterModel>
    <div class="footer">
    @Html.Raw(Model.FooterText)
    </div>
    ---
    But, right now I have to do this:
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    <div class="footer">
    @Html.Raw(Model.Content.GetProperty("footerText").Value))
    </div>
    In other words, I would like to fully utilize the MVC structure. And in a model-view-controller composition, the logic is placed in the model, and the controller makes sure you get what you asked for.

  • Charles Afford 1163 posts 1709 karma points
    Apr 03, 2013 @ 22:06
    Charles Afford
    0

    Yep you can do that.

    So you have your partialview say which at the top will have:

    YOUR MODEL >> this will be in a class liabary somewhere that is built into your www root bin.  This could for instance have property IENUM of all your node properties.

    YOUR SUFACE CONTROLLER >> this will call your method to get the IENUM and return it to the partial view

    YOUR VIEW >> this will create a new instance of the model and populate the model IENUM property

    YOUR PARTIAL VIEW >>  at the top of this you will have something like:

    @model YOURNAMESPACE.YOURMODEL

    and then

    foreach(item in YOURMODEL.YOUR PROPERTY)

    {

    logic

    }

     

    Is this what you were you after?  Please let me know if you need some more help :)  Charlie

  • Kristian Kjær 6 posts 56 karma points
    Apr 04, 2013 @ 09:09
    Kristian Kjær
    0

    Hi Charlie,

     

    I might need further help to understand what you mean :-)

     

    I already have a class 'Footer', which inherits from 'RenderModel'. My model has this property:

    public string FooterText { get; set; }

    I want to populate that with the real text ofc ;) But I don't know what you mean by "SUFACE CONTROLLER"...

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 04, 2013 @ 09:56
    Dave Woestenborghs
    0

    You can find the documentation of surface controllers here : http://our.umbraco.org/documentation/Reference/Mvc/surface-controllers

    Dave

  • Charles Afford 1163 posts 1709 karma points
    Apr 04, 2013 @ 09:58
    Charles Afford
    0

    Hi Kristian :) 

    Here is some information about SurfaceControllers and MVC with Umbraco

    http://our.umbraco.org/documentation/Reference/Mvc/surface-controllers

    http://our.umbraco.org/documentation/Reference/Mvc

    It is just away of using a controller in the context of an umbraco application.  

     

    Say you have a FooterModel:

     

    using umbraco.NodeFactory;

    namespace

    {

    public class FooterModel

    {

    //property

    public string FooterText {get;set;}

     

    //const of the property alias in Umbraco

    const string FOOTERPROPERTYALIAS = "footertext";

     

    //some method to populateproperty

    public string FooterText()

    {

    //get contextnode

    Node contextNode = Node.GetCurrent();

     

    //assign property from umbraco to public property or use the default if nothing is there

    FooterText = String.IsNullOrEmpty(contextNode .GetProperty(FOOTERPROPERTYALIAS ).Value.ToString())  ? "defaultifempty" : contextNode .GetProperty(FOOTERPROPERTYALIAS ).Value.ToString()

    }

    }

     

    Then (i presume your HTML view) the place where you call renderBody()

    call the partial and pass in a new instance of your footer model

    @Html.RenderPartial("Footer", new FooterModel());

     

    //FOOTER PARTIAL VIEW "FOOTER"

    @modelyournamespace.FooterModel

    @{

    Page,Title = "something";

    }

    <h1>@Model.FooterText</h1>

     

    Does that make sense? :) 

  • Kristian Kjær 6 posts 56 karma points
    Apr 04, 2013 @ 11:25
    Kristian Kjær
    100

    Thanks for your replies!

     

    However, I have to make sure you know I mean 'Partial View Macro' and not the ordinary 'Partial View' from Umbraco 4+.

     

    I contintued to read about surface controllers and found this article http://www.proworks.com/blog/2013/02/07/umbraco-6-what's-new/

     

    So I setup my FooterSurfaceController:

     

    public class FooterController : SurfaceController

        {

            public PartialViewResult Footer()

            {

                Footer footerModel = new Footer();

     

                return PartialView(footerModel);

            }

        }

     

    And kept my Footer model as it was, implementing the logic needed in its constructor.

     

    My Footer Partial Macro View is then defined like this:

     

    @{

        Layout = null;

    }

    @model namespace.Footer

     

    <div class="footer">

        @Html.Raw(Model.FooterText)

    </div>

     

    And this is much cleaner than before. Now my view does its only job - to present data, not retrieve AND present.

  • Charles Afford 1163 posts 1709 karma points
    Apr 04, 2013 @ 23:22
    Charles Afford
    0

    Hi, not totally sure what you have done?  could you explain it?  How is your surface controller working?  It does not appear to be going anywhere?  Charlie :)

Please Sign in or register to post replies

Write your reply to:

Draft