Copied to clipboard

Flag this post as spam?

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


  • Matthew 138 posts 201 karma points
    Apr 27, 2014 @ 08:09
    Matthew
    0

    Passing parameters from one partial to another

    For a partial that gets child pages, I wrote a if{elseif{else in a foreach loop that checks for images, and it works well so I'd like to make it a partial and reuse it in other, similar partials. I haven't been able to find a good doc or example to get me started, mostly the v.7 docs seem to be about parameters in macros.

    I think it could be as simple as: @Html.Partial("imageCheck", childPage), childPage being the var from the foreach loop, and then something like a var childPage = ViewData["childPage"];but that doesn't seem to bring the childPage data along.

    Any tips, pointers, good jokes, whatever?

    Thanks much.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Apr 28, 2014 @ 12:15
    Jeroen Breuer
    0

    Hello,

    Did you read the documentation?

    Maybe this can also help: http://www.codenoevil.com/pass-additional-viewdata-asp-net-mvc-4/

    Jeroen

  • Matthew 138 posts 201 karma points
    Apr 29, 2014 @ 03:52
    Matthew
    0

    Hi,

    I did read that, quite a few times, but it is still at too high a level for me. For instance, under Strongly Typed PV's, it says, you just need to do this: @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyModel> but I can't figure out what <MyModel> represents. Elsewhere in life, those <> indicate a replaceable value but in some places in Umbraco, the <> are required. Regardless, I've not found a combination either way that makes it past intellisense.

    Later, it says if you are passing around instances if IPublishedContent, just inherit from Umbraco.Web.Mvc.UmbracoTemplatePage, with @Html.Partial(MyPartialName", child) but doesn't say how to use that in the partial. So far, my efforts there have not gotten past intellisense either, maybe because I don't know what to do with that first <MyModel>.

    I've also tried variations on the Getting Started docs, which include promising bits similar to what I've seen in Kevin's LocalGov package:

    @Html.Partial("YourPartialName", new ViewDataDictionary{{ "age",33},{"name","peter"}})
    

    retrieved with ViewData["age"]but again, no love for me, 'the name 'age' does not exist in the current context', etc.

    I looked at the 'Pass Additional ViewData to an ASP.NET MVC 4 Partial... doc as well but that looks like the wrong way is what everything else suggests is the right way, I don't think I can make sense of it from where I'm at.

    Also watched the vid linked in the first docs referenced, How Do I Work with Data in ASP.NET MVC PV's, but that was from 2009 and 15 of its 20 minutes were watching him type model data, I fell asleep twice. It is also a concern that old information like that is likely to include some syntactical obsolescence that will devour another half a day of ignorant disenlightenment.

    I know I'm weak on what are probably fundamentals for what I'm trying to do but it seems so close...

    Thanks for trying though, I appreciate your taking the time. If you have a recommendation for a good book on the subject (ASP.NET, I assume), I'd be glad to purchase it. I perused a few and didn't find anything that looked like it would have the answer to sending my 'childPage' to a little 'if' filter.

  • Matthew 138 posts 201 karma points
    Apr 29, 2014 @ 04:51
    Matthew
    1

    So, I have this in the first partial:

    @foreach (var childPage in nodes)
    {
      <div>
        @Html.Partial("uImage", new ViewDataDictionary{{"uPage", childPage},{"randomVar", "totallyrandom"}})
      </div>
    }
    

    then in the uImage partial, this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    var someVar = ViewData["uPage"];
    var anotherVar = ViewData["randomVar"];
    
    <p>@someVar</p>
    <p>@anotherVar</p>
    

    and it renders this in the browser:

    Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent.

    totallyrandom

    So the value 'totallyrandom' is coming over fine in 'randomVar' and the value of 'uPage' appears to match up as well.

    However, if I change the second partial's '@someVar' to '@someVar.Name', intellisense says 'Cannot convert method group...', so all is not well. Other variations have also gone down in flames.

    I'm guessing it has to do with the value coming over as a string but no clue where to go next. Any idea?

  • Matthew 138 posts 201 karma points
    Apr 29, 2014 @ 07:02
    Matthew
    5

    Holy crapnoodles, I think I figgered it oot:

    In partial 1:

    @Html.Partial("uImage", new ViewDataDictionary{{"uPage", childPage.Id}})
    

    In partial 2:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{    
        var childPage = @Umbraco.TypedContent(ViewData["uPage"]);
    }
    
    @if (childPage.HasValue("featureImage"))
        {
            var mediaID = childPage.GetPropertyValue<int>("featureImage");
            var mediaTyped = Umbraco.TypedMedia(mediaID);
            <a href="@childPage.Url"><img src="@mediaTyped.Url" /></a>
        }
        else if (childPage.HasValue("topImage"))
        {
            var mediaID = childPage.GetCropUrl("topImage", "FullSizeBanner");
            var mediaTyped = Umbraco.TypedMedia(mediaID);
            <img src="@childPage.GetCropUrl("topImage", "BigImage")" alt="" />
        }
        else
        {
            <img src="~/assets/Mitchell-logo-pos-hor.gif" />
        }
    

    So far it seems to be rendering properly. Anything wrong with that?

  • Amir Khan 1282 posts 2739 karma points
    Apr 07, 2015 @ 17:07
    Amir Khan
    0

    Hi Matthew, how are you checking if childPage.Id is null? I can't seem to find a way to make sure it actually contains something?

    Thanks,
    Amir

Please Sign in or register to post replies

Write your reply to:

Draft