Copied to clipboard

Flag this post as spam?

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


  • Nancy A. 44 posts 175 karma points
    May 06, 2019 @ 13:33
    Nancy A.
    0

    Can't retrieve property value within a partial view

    For this specific project, I'm working with partial views to create different panels for the home page in a website. Using Umbraco's backoffice, I created a document type for the home page, and using the composition feature, now have one document property - a text box (which will be used to pass in a link from the user to the website) - am starting slow here. There is a corresponding template for the homepage as well.

    Now I'm in VS, and am trying to get content from that property value in the _sidepanelpoll.cshtml file (that is rendered in the home.cshtml file via @{ Html.RenderAction("RenderSidePanelPoll", "Home");} But the @Model.Value("link") - 'link' is the alias for the property in the Home document type - isn't working.

    I'm trying to use @inherits Umbraco.Web.Mvc.UmbracoViewPage AND @using ContentModels = Umbraco.Web.PublishedModels; but still throw an System.NullReferenceException exception and my VS is showing a squiggly red line under .Home of the inherits statement - I suspect I'm not using the right model name (or should I avoid using a strongly typed model here given it's a partial view?).

    I want to use @Umbraco.AssignedContentItem.GetPropertyValue('link') along with @inherits UmbracoViewPage at the top but that's no longer available in Umbraco 8. What are my options? Thank you!

    Edited to add - I noticed my App_Data folder is grayed out; when I right-click and select, 'Include in Project', I get all kinds of duplicate errors (e.g., The type 'Home' already contains a definition for 'Model TypeAlias', etc) for the models.generated.cs file. Not sure if this is related in any way.

  • Jonathan Distenfeld 105 posts 618 karma points
    May 07, 2019 @ 11:28
    Jonathan Distenfeld
    100

    Hi,

    you have to pass the model to the partial view in order to access the properties. You can achieve this by rendering the partial like this

    @Html.Partial("_sidepanelpoll", Model)
    

    or, if you have to call the controller action for some reason, you can add a parameter to your action and pass it like this:

    @{ Html.RenderAction("RenderSidePanelPoll", "Home", new { model = Model });}
    

    Then, in your Controlleraction, you can pass the model to the view like this:

    return PartialView("_sidepanelpoll", model);
    

    Regarding the duplicate errors, I need some more information. Please provide the code where the error occurs. I assume that these errors come from the modelsbuilder.

    Jonathan

  • Nancy A. 44 posts 175 karma points
    May 07, 2019 @ 12:30
    Nancy A.
    0

    Thanks Jonathan, you're right - passing the model to the partial view did the trick (why didn't I think of this, I don't know. Oh Umbraco ...).

    As for the App_Data folder, when I include it in the project and do a build, I get all kinds of 'duplicates! you've got duplicates!' errors for the models.generated.cs file. I've included a snapshot for your review:

    enter image description here

  • Jonathan Distenfeld 105 posts 618 karma points
    May 07, 2019 @ 13:52
    Jonathan Distenfeld
    0

    Hi Nancy,

    I'm glad it helped!

    looks like your problem is caused by the modelsbuilder. Take a look at this thread: https://our.umbraco.com/forum/using-umbraco-and-getting-started/78340-error-cs0579-duplicate-publishedcontentmodel-attribute

    Probably you are currently not using the modelsbuilder, so I see two possible solutions:

    1. Don't use the ModelsBuilder (deactivate the ModelsBuilder or exclude the Files)
    2. Use the ModelsBuilder (which is my preferred way since you have strongly typed models and therefore also intellisense)

    For more information about the ModelsBuilder, take a look at this Doc: https://our.umbraco.com/documentation/Reference/Templating/Modelsbuilder/

    Jonathan

  • Nancy A. 44 posts 175 karma points
    May 07, 2019 @ 17:58
    Nancy A.
    1

    You are awesome. Thank you, will be reviewing these links.

Please Sign in or register to post replies

Write your reply to:

Draft