Copied to clipboard

Flag this post as spam?

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


  • Stefan Karlsson 14 posts 64 karma points
    Mar 01, 2013 @ 09:47
    Stefan Karlsson
    0

    Razor Partial View Macro (v. 6)

    Hi!

    Iam really new to Umbraco but have used the MVC pattern with razord syntax before.

    What i iam trying to do is to list all my properties in a list.
    I have added serveral page's created with a specified document type that i want to loop to get all propertie values.

    Im trying to achive this with a partial view macro, but cant get it to work.

    Alias of Document type: ExtremeNewsItem
    Calling Macro in Partial View: @Umbraco.RenderMacro("ExtremeStartpageList")

    This is my properties i want to loop:

    extremeNewsHeadline

    extremeDatePosted


    This is how i want the macro:

    @inherits Umbraco.Web.Macros.
    <ul>
    @foreach (var item in Model.ExtremeNewsItem.Children.OrderBy("extremeDatePosted")) 
    {
        <li>
    <span>@Umbraco.Field("extremeDatePosted")</span>
    <br />
        <a href="@item.UrlToPage">@Library.Truncate(extremeNewsHeadline,100)</a>
        </li>
    }
    </ul>

     

  • gary 385 posts 916 karma points
    Mar 01, 2013 @ 11:19
    gary
    0

    Hi Stefan

    Not sure there is enough here to give you a full answer - would need to know Umbraco version ie 4.11 etc and if you are using in Webforms or Mvc mode.

    But just some general points - 

    @foreach(var item inModel.ExtremeNewsItem.Children.OrderBy("extremeDatePosted"))

    should be Model.Children.OrderBy - but your document alias should be as written - ie ExtremeDatePosted, if first letter is capital, then it must be.

    <span>@Umbraco.Field("extremeDatePosted")</span>

    When you call a field, even thought the alias first letter is not a Capital, the call must be a Capital, therefore "ExtremeDatePosted"

    <a href="@item.UrlToPage">@Library.Truncate(extremeNewsHeadline,100)</a>

    Dependent upon the version, you may need to call @item.Url(), or item.Url, or item.NiceUrl() or item.NiceUrl. Also it is odd, but the Umbraco Library is not a capital, so @library.

    I think you code looks to be OK, in terms of getting what you require, it just seems some of the "Umbraco" sensitive points need a tweek.

    There are many ways to make Umbraco work, with a little more time you will begin to feel more comfortable.

    Hope it helps G

     

     

  • Stefan Karlsson 14 posts 64 karma points
    Mar 01, 2013 @ 11:55
    Stefan Karlsson
    0

    Hi & Thanks for your response gary.

    I have changed the code but i still can't get the macro to render. I use Umbraco 6 with MVC.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    <ul>
            @foreach (var item in Model.Children.OrderBy("ExtremeDatePosted")) 
            {
    <li>
    <span>@Umbraco.Field("ExtremeDatePosted")</span>
    <br />
    <a href="@item.NiceUrl()">@Library.Truncate(ExtremeNewsHeadline,100)</a>
    </li>
            }
    </ul>
  • gary 385 posts 916 karma points
    Mar 01, 2013 @ 12:06
    gary
    0

    Hi

    Ok if V6 Mvc, it would be easier to know what error you are getting.

    It looks to me that maybe you could use as just a partial? If you first get to work as a partial, then if you need as a macro, at least you know the code is working.

    For the partial, call @Html.Partial("nameOfPartial") from the view.

    In partial view

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    <ul>

            @foreach (var item in CurrentPage.Children.OrderBy("ExtremeDatePosted"))  moved to dynamic CurrentPage, or use Model.Content.Children

            {

    <li>

    <span>@item.ExtremeDatePosted</span> using dynamic @CurrentPage or @Model.Content.GetPropertyValue("ExtremeDatePosted")

    <br />

    <a href="@item.Url()">@Umbraco.Truncate(ExtremeNewsHeadline,100)</a>

    </li>

            }

    </ul>

    If you use Model.Content.   you will get Intellisense in VS, or WebMatrix, if this helps, but it doesn't show for dynamic @CurrentPage

    Ok, if you can get that to run in a partial - you will be heading in the right direction.

    Regards G

     

  • Stefan Karlsson 14 posts 64 karma points
    Mar 01, 2013 @ 12:52
    Stefan Karlsson
    0

    I have tryed and tryed.

    I cant even run this in my partial view, simple text works good in partial view.

    @foreach (var childPage in CurrentPage.Children())
                      {
                        @:test
                      }
  • Stefan Karlsson 14 posts 64 karma points
    Mar 03, 2013 @ 22:55
    Stefan Karlsson
    0

    Ok ill try to ask again.

    This is my layout three:

    ExtremeStartpageLayout
    - ExtremeStartpageContent (Partial: _ExtremeNewsList.cshtml) 

    When i use this i got no error, but i also doesent get any thing looping in the partial view, what am i doing wrong?

    ExtremeStartpageLayout:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{

        Layout = null;

    }

    <!DOCTYPE html>

    <html>

    <head>

    <title>@Umbraco.Field("pageTitle")</title>

    <link rel="stylesheet" type="text/css" href="/css/reset.css">

    <link rel="stylesheet" type="text/css" href="/css/ExtremeNews.css">

    </head>

    <body>

    <div id="pageWrapper">

    <header>

    <div id="leftTopImage"></div>

    <div id="logoType">

    <img src="/media/48943/logotype.jpg" alt="logotype." />

    </div>

    </header>

     

    <div id="contentWrapper">

    @RenderSection("Content", true)

    </div>

    </div>

    </body>

    </html>

    ExtremeStartpageContent

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = "ExtremeStartpageLayout.cshtml"; } @section Content { <section id="content"> <article> <h1>@Umbraco.Field("extremeNewsHeadline")</h1> @Umbraco.Field("extremeNewsText") <p> <div id="uploadedFile"> <img src="Umbraco/images/umbraco/@Umbraco.Field("extremeFileIcon")" /><a [email protected]("extremeFileUpload")>@Umbraco.Field("extremeLinkText")</a> </div> </p> </article> </section> <aside> <h2>Senaste nytt</h2> <nav> @Html.Partial("_ExtremeNewsList") </nav> </aside> }

    _ExtremeNewsList

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic> @inherits Umbraco.Web.Mvc.UmbracoTemplatePage <ul> @foreach (var item in Model.Content.Children()) { <li> <span>@Umbraco.Field("pageTitle")</span> <br /> <a href="@item.Url()">@item.Name</a> </li> } </ul>
  • gary 385 posts 916 karma points
    Mar 03, 2013 @ 23:02
    gary
    0

    Hi Stefan

    First thing is you need an @RenderBody() in your layout page, but I am surprised you did not get an error for this. Is anything at all displaying?

    G

  • gary 385 posts 916 karma points
    Mar 03, 2013 @ 23:25
    gary
    0

    Just some thoughts, your extremeStartPage template needs to be assigned to your documentType, go to Content Section and then run from the properties tab, where you will see the template type listed, then under is the link to the page, there is a link and an alternative link.

    As mentioned previously, your fields, when call need to begin with capital letter, so you have called pageTitle, as per the alias, but it must be PageTitle. 

    First just make something simple work, document type attached to template, call a layout in this, and call a simple textstring field.

    If it would be of more use, there is a good sample site that Jeroen put together for DAMP, it would be worth downloading and looking at the structure.

    http://24days.in/umbraco/2012/damp-gallery/

    It is on that link and can open as a VS project, this again may help you in relating the MVC with the Umbraco SetUp.

    Hope this can help you

    G

     

Please Sign in or register to post replies

Write your reply to:

Draft