Copied to clipboard

Flag this post as spam?

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


  • Gert 81 posts 288 karma points
    Apr 26, 2014 @ 22:44
    Gert
    0

    Cannot access @Umbraco.Field in partial view that calls controller in Umbraco 7

    Hi, I created a simple Contact Form in umbraco 7 with a Surface controller, a parital View and a Partial macro view. The partial view is a typical view like this:

    @using Umbraco.Web
    @model Umbraco7Cms.ViewModels.ContactViewModel
    
    @if ( TempData[ "success" ] == null )
    {
        using ( Html.BeginUmbracoForm( "SubmitContactForm", "Communication" ) )
        {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary( true )
    
    
    ... labels and input fields come here
    
    }
    else
    {
        <a href="#confirmation"></a>
        <p>@Umbraco.Field("EmailConfirmationText")</p>
    }
    

    I want to be able to access the document properties but the call @Umbraco.Field() is unknown allthough I included the Umbraco.Web namespace.

    How can I access the document properties and dictionary items in from within this partial view? Thank you very much.

    Kind Regards, Gert.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 26, 2014 @ 22:50
    Jan Skovgaard
    0

    Hi Gert

    Seems like some of your post is missing?

    /Jan

  • Gert 81 posts 288 karma points
    Apr 26, 2014 @ 22:55
    Gert
    0

    Hi Jan, I completed it in the mean while ;-). Thanks!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 26, 2014 @ 23:00
    Jan Skovgaard
    0

    Hi Gert

    You should be able to access document type properties by writing @CurrentPage.propertyname and in regards to fetching dictionary items I think this piece of documentation can help you http://our.umbraco.org/documentation/reference/templating/macros/razor/using-dictionary-items

    You can read more about accessing the properties here as well http://our.umbraco.org/documentation/reference/Templating/Mvc/views

    Hope this helps :)

    /Jan

  • Gert 81 posts 288 karma points
    Apr 26, 2014 @ 23:11
    Gert
    0

    Hi Jan,

    This does not work because the partial view does not inherit from Umbraco.Web.Mvc.UmbracoTemplatePage. It defines a viewmodel (@model Umbraco7Cms.ViewModels.ContactViewModel).

    This view is rendered in a Macro Partial View like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco7Cms.ViewModels;
    @{
        Html.RenderPartial( "~/Views/Partials/contactForm.cshtml", new ContactViewModel() );
    }
    

    So the ViewModel can be passed. So when I use @CurrentPage.propertyname, I get an error stating that CurrentPage does not exist.

    Any idea how to make this work?

    Thanks Gert.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 26, 2014 @ 23:13
    Jan Skovgaard
    0

    Aah, Sorry Gert - Misunderstood your question!

    If I figure out how to deal with it I will let you know but my guess is that Jeavon (Or some of the other bright minds in here) will provide an answer before I manage to find out :D

    Cheers, Jan

  • Gert 81 posts 288 karma points
    Apr 27, 2014 @ 00:07
    Gert
    0

    OK, thanks Jan!

    Cheers, Gert.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 27, 2014 @ 22:49
    Jeavon Leopold
    2

    @Jan, sounds like a carrot on a stick :-)

    @Gert, you need to replace your @model Umbraco7Cms.ViewModels.ContactViewModel with @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco7Cms.ViewModels.ContactViewModel> now you can access the UmbracoHelper on @Umbraco but your partial view still expects your custom Model

    Jeavon

  • Gert 81 posts 288 karma points
    Apr 28, 2014 @ 08:21
    Gert
    0

    Oh, it's as simple as that! Thanks Jeavon!

    Regards, Gert

  • Gert 81 posts 288 karma points
    Apr 28, 2014 @ 08:24
    Gert
    1

    Jeavon, I tried to give you a high five, but apperently it doesn't get saved. Every time I refresh the browser the "You rock!" disappears ... nevertheless: you rock! ;-)

    Gert.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 28, 2014 @ 09:53
    Jeavon Leopold
    100

    No worries, I think maybe you need 70 karma to h5 but I'm not sure, @Jan might know?

    However you should be able to mark the solution using the green tick on the correct answer.

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft