Copied to clipboard

Flag this post as spam?

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


  • zoky_88 5 posts 85 karma points
    Apr 26, 2019 @ 11:14
    zoky_88
    0

    Render different partial view for different Url Page

    I have 2 forms and 2 pages. I want to use the same document type and render different form on the page, depending on which page the users will land on.

    Simple but difficult task for me as of yet. I am getting the page ID and setting it to a variable, but when I check it in the if statement it's complaining that it can't conver type "bool" to IPublished.Content....

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<CtaForm>
    @{
        var thunderPage = Umbraco.TypedContent(2209);
        var downloadGuidePage = Umbraco.TypedContent(2214);
    }
    
    @if (thunderPage = true)
    {
        @Html.Partial("Forms/_CtaForm", new CitytalkWebsite.Models.ContactForm())
    }
    else if (downloadGuidePage = true))
    {
        @Html.Partial("Forms/_DownloadPageForm", new CitytalkWebsite.Models.ContactForm())
    }
    
  • Weber 12 posts 36 karma points
    Apr 26, 2019 @ 11:54
    Weber
    1

    If you need to compare to specific page, you can:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<CtaForm>
    @{
        var thunderPage = 2209;
        var downloadGuidePage = 2214;
    }
    
    @if (thunderPage == (int)CurrentPage.Id)
    {
        @Html.Partial("Forms/_CtaForm", new CitytalkWebsite.Models.ContactForm())
    }
    else if (downloadGuidePage == (int)CurrentPage.Id))
    {
        @Html.Partial("Forms/_DownloadPageForm", new CitytalkWebsite.Models.ContactForm())
    }
    

    Did not check spelling :) Bare in mind this is not a very scallable solution.

    Other senario could be to use a macro with parameter to pick correct form

  • zoky_88 5 posts 85 karma points
    Apr 26, 2019 @ 12:05
    zoky_88
    0

    Hmm, your solution gives me this error:

    The name 'CurrentPage' does not exist in the current context

  • Weber 12 posts 36 karma points
    Apr 26, 2019 @ 12:15
    Weber
    1

    Sry.. wasn't to sharp there :)

    When you use UmbracoViewPage CurrentPage isn't awailable

    Use Model.Content.Id to get Id of current

    or just add

    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
  • zoky_88 5 posts 85 karma points
    Apr 26, 2019 @ 13:24
    zoky_88
    0

    No worries, thanks a lot for trying to help.

    You can just use Model.Id in ViewPage, Model.Content.Id is if you use TemplatePage, if I got this right.

    I changed the code with Model.Id, no errors on build or in console, both pages load fine but the forms are not rendered on them at all? :( Not sure how to proceed further....thinking about giving up and just create another document type that will render the second form on download guide page.

  • Rhys Hamilton 140 posts 942 karma points
    Apr 26, 2019 @ 13:37
    Rhys Hamilton
    100

    An alternative solution would be to create another template for your document type.

    For example, you could have a generic ContactForm Template and then a DownloadPageForm Template which both contain the relevant partial view.

    In this way, you won't need to get the page id, but instead just set the appropriate template in the back-end.

    ContactForm.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<CtaForm>
    @Html.Partial("Forms/_CtaForm", new CitytalkWebsite.Models.ContactForm())
    

    DownloadPageForm.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<CtaForm>
    @Html.Partial("Forms/_DownloadPageForm", new CitytalkWebsite.Models.ContactForm())
    
Please Sign in or register to post replies

Write your reply to:

Draft