Copied to clipboard

Flag this post as spam?

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


  • Phillip Turner 98 posts 412 karma points
    Apr 07, 2014 @ 01:35
    Phillip Turner
    0

    Getting a field value into a partial view using razor

    Hello Umbracians.

    I need a little guidance with a Partial View using MVC, Umbraco 6.1.6.

    My documentType structure is as follows:

    Master > SubMaster > Page

    Page inherits a property from SubMaster called "pageHeroImage" of type Media Picker.

    I have a Partial View that renders on Pages Template (Code Below) and I need to know how to get the field values. I know I can get it via @Umbraco.Content(id) and have been successful, but I want to use this partial view on other pages as well and I do not want to have to create new partial views for each Template I create.

    Is there a way I can get this value?

    This is my working code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var page = Umbraco.Content(1223);
    }
    <div id="hero-container-subpage" class="border-style">
        <div id="left-side-menu-subpage">
            @Html.Partial("mes-mini-menu")
            @Html.Partial("mini-heros")
    </div>
        <div id="hero-panel-subpage">
            <img src="@Umbraco.Media(page.pageHeroImage).Url" />
        </div>
    </div>
    

    Any help would be greatly appreciated.

    Phillip

  • Phillip Turner 98 posts 412 karma points
    Apr 07, 2014 @ 04:46
    Phillip Turner
    0

    Ok, so I added this code and got the result I needed as long as the Page is at the 2nd level (which it should be)

    var page = CurrentPage.AncestorOrSelf(2);
    

    So the question now is... is there a better way?

    Thanks

    Phillip

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 07, 2014 @ 10:36
    Jeavon Leopold
    1

    Hi Phillip,

    I think your approach is fine. However if you did want to make you partial view really flexible you could pass the model into it. However normally when you do this you need to use the strongly typed model (Model.Content) instead of dynamics (CurrentPage) but you can work around this as shown below (not widely tested).

    MyPartial.cshtml

    @inherits UmbracoViewPage<IPublishedContent>
    
    @{
        var CurrentPage = Model.AsDynamic();        
    }  
    
    <div id="hero-container-subpage" class="border-style">
        <div id="left-side-menu-subpage">
            @Html.Partial("mes-mini-menu")
            @Html.Partial("mini-heros")
        </div>
        <div id="hero-panel-subpage">
            <img src="@Umbraco.Media(CurrentPage.pageHeroImage).Url" />
        </div>
    </div>
    

    MyView.cshtml

    @{
        var page = Model.Content.AncestorOrSelf(2);
    }
    @Html.Partial("MyPartial", page)
    

    Or if you really want to keep everything Dynamic:

    @{
        var page = CurrentPage.AncestorOrSelf(2);
    }
    @Html.Partial("MyPartial", (IPublishedContent)page)
    

    Jeavon

  • Phillip Turner 98 posts 412 karma points
    Apr 09, 2014 @ 16:32
    Phillip Turner
    0

    Hi Jeavon.

    I like your approach better, but it rendered the following error:

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'Umbraco.Core.Models.IPublishedContent'.
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 09, 2014 @ 16:35
    Jeavon Leopold
    0

    Hi Phillip,

    Which code snippet did you use in your view? The @Html.Partial bit

    Jeavon

  • Phillip Turner 98 posts 412 karma points
    Apr 09, 2014 @ 16:51
    Phillip Turner
    0

    Im sorry! I opted for the first

    @inherits UmbracoViewPage<IPublishedContent>
    @{
    var CurrentPage = Model.AsDynamic();        
    }  
    <div id="hero-container-subpage" class="border-style">
        <div id="left-side-menu-subpage">
            @Html.Partial("mes-mini-menu")
            @Html.Partial("mini-heros")
        </div>
        <div id="hero-panel-subpage">
            <img src="@Umbraco.Media(CurrentPage.pageHeroImage).Url" />
        </div>
    </div>
    
  • Phillip Turner 98 posts 412 karma points
    Apr 09, 2014 @ 16:52
    Phillip Turner
    0

    Hold on... Duh! I didn't pass the page when rendering the view. Standby!!!

  • ibrahim TUNC 55 posts 133 karma points
    Jan 07, 2015 @ 22:11
    ibrahim TUNC
    0

    I'm having this problem too.
    solution page?

Please Sign in or register to post replies

Write your reply to:

Draft