Copied to clipboard

Flag this post as spam?

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


  • Magnus Thygesen 8 posts 88 karma points
    Nov 23, 2018 @ 13:46
    Magnus Thygesen
    0

    Using @inherits UmbracoTemplatePage<IPublishedContent> and Custom model

    I am trying to use a @inherits UmbracoTemplatePage

    I dont know if this is possible?? What i am trying to do, is make a partial view, that takes both CMS content therefor IpublishedContent, and database data, and therefor also use UmbracoWebsite.Models.Coffee

    My Partial View looks like this

    @inherits UmbracoTemplatePage<IPublishedContent>
    @inhertis Umbraco.Web.Mvc.UmbracoViewPage<UmbracoWebsite.Models.Coffee>
        @{
        ViewBag.Title = "GetCoffee";
        }
        <section class="section--themed">
            <div class="container">
                <div class="row">
                    <div class="ta-center col-md-4">
                        <ui>
                            <p class="footerParagraph">@Model.Content.GetPropertyValue("locationAddress")</p>
                            <p class="footerParagraph">@Model.Content.GetPropertyValue("cVRNumber")</p>
                        </ui>
                    </div>
                    <div class="ta-center col-md-4">
                        @{
                        var typedRelatedLinksConverted = Model.Content.GetPropertyValue<RelatedLinks>
                            ("facebookLink");
    
                            if (typedRelatedLinksConverted.Any())
                            {
                            <div>
                                @foreach (var item in typedRelatedLinksConverted)
                                {
                                var linkTarget = (item.NewWindow) ? "_blank" : null;
                                <p><a href="@item.Link" target="@linkTarget">@item.Caption</a></p>
                                }
                            </div>
                            }
                            }
                            <div class="ta-center col-md-4">
                                <p>
                                    @foreach (var item in Model)
                                    {
                                    <tr>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.Company.CompanyName)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.Name)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.Volume)
                                            ml
                                        </td>
                                        <td>
                                            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                                            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                                            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                                        </td>
                                    </tr>
                                    }
    
                                    </table>
                                </p>
                            </div>
                    </div>
                </div>
        </section>
    

    The first two <div class="ta-center col-md-4"> shows cms content with IpublishedContent

    And the last <div class="ta-center col-md-4"> should show content using my UmbracoWebsite.Models.Coffee model

    Anyone have any ideas?

    I might be worth note that i am using a surfaceController, which i call from my master.cshtml @Html.Action("GetCoffee", "UmbracoSurface")

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Nov 23, 2018 @ 15:38
    Nik
    0

    Hi Magnus,

    I seem to think you can only have one @imports statement.

    But, you should be able to access your parent IPublishedContent type via:

    @Umbraco.AssignedContentItem in the partial view.

    Nik

  • Magnus Thygesen 8 posts 88 karma points
    Nov 26, 2018 @ 08:44
    Magnus Thygesen
    0

    Hey Nik, so this is what i got.

        <section class="section--themed">
        <div class="container">
            <div class="row">
                <div class="ta-center col-md-4">
                    <ui>
                        <p class="footerParagraph">@Umbraco.AssignedContentItem.GetPropertyValue("locationAddress")</p>
                        <p class="footerParagraph">@Umbraco.AssignedContentItem.GetPropertyValue("cVRNumber")</p>
                    </ui>
                </div>
                <div class="ta-center col-md-4">
                <p>FUUUUUUUCK!</p>
    
                    @{
                    var typedRelatedLinksConverted = Umbraco.AssignedContentItem.GetPropertyValue<RelatedLinks>
                        ("facebookLink");
    
                        if (typedRelatedLinksConverted.Any())
                        {
                        <div>
                            foreach (var item in typedRelatedLinksConverted)
                            {
                            var linkTarget = (item.NewWindow) ? "_blank" : null;
                            <p><a href="@item.Link" target="@linkTarget">@item.Caption</a></p>
                            }
                        </div>
                        }
                        }
    

    Now it says CS0117: 'umbraco.item' indeholder ikke en definition af 'Link'. and the second part is, when i comment it out, it want show my content in these lines

    <p class="footerParagraph">@Umbraco.AssignedContentItem.GetPropertyValue("locationAddress")</p>
    <p class="footerParagraph">@Umbraco.AssignedContentItem.GetPropertyValue("cVRNumber")</p>
    

    Its just blank.

    So to sum up my very confusing message

    I cant find link when i try to do a foreach on a umbraco item, and @Umbraco.AssignedContentItem.GetPropertyValue, doesn't show any data on my page

  • Magnus Thygesen 8 posts 88 karma points
    Nov 26, 2018 @ 10:55
    Magnus Thygesen
    0

    Just for further notice. The solution become that i created a partial view macro the used @model, and parsed it as a @Html.Action

  • Laura Weatherhead 25 posts 153 karma points MVP 6x c-trib
    Nov 26, 2018 @ 16:16
    Laura Weatherhead
    1

    Hey Magnus,

    Overall I'm not quite sure what you're looking for, and it appears you have something working nevertheless!

    The original question you posted sounds like you're looking for the RenderModel capabilities that Umbraco offers where the IPublishedContent model that comes back from the CMS can be extended with custom properties.

    There's some great documentation on this functionality here: https://our.umbraco.com/documentation/Reference/Routing/custom-controllers#returning-a-view-with-a-custom-model which steps you through extending the base model and inheriting in on the page, thus allowing the custom properties I think you were looking for :)

    Cheers,

    Laura

  • Magnus Thygesen 8 posts 88 karma points
    Nov 28, 2018 @ 11:00
    Magnus Thygesen
    0

    Hey Laura. I can't seem to get it to work.

    This is my Model

    namespace WebAPITestConnectionToOtherSite.Models
    {
        public class Coffee : RenderModel, IEnumerable<Coffee>
        {
        public Coffee(IPublishedContent content) : base(content) { }
    
        public int Id { get; set; }
        public string Name { get; set; }
    
        public int Volume { get; set; }
    
    
        public int CompanyId { get; set; }
        public Company Company { get; set; }
    
        public IEnumerator<Coffee> GetEnumerator()
        {
            throw new NotImplementedException();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
    
    public class Company
    {
    
        public int Id { get; set; }
    
        public string CompanyName { get; set; }
    }
    

    }

    My Controller method is this.

    public ActionResult GetCoffee(RenderModel model)
        {
            var apiCall = $"Home/Coffee";
    
            var model1 = GetFromAPI<List<Coffee>>(apiCall);
            model1.And(model);
    
            return PartialView("GetCoffee", model1);
        }
    

    When i try to render my view it saying

    @Html.Action("GetCoffee()" , "MySurface")
    

    i get an exception saying System.InvalidOperationException: 'No route in the route table matches the supplied values.'

    My troubles is that i have to get data via an API call, return it. Add it to a view, with umbraco content and my custom model content aswell. I need to be able to insert my data wherever i want..

    <div>
          <div>Custom model content</div>
          <div>Umbraco content</div>
          <div>Custom model content</div>
    
    </div>
    
  • Laura Weatherhead 25 posts 153 karma points MVP 6x c-trib
    Nov 29, 2018 @ 11:42
    Laura Weatherhead
    0

    Hey Magnus,

    You probably want your @Html.Action call to your MySurfaceController to look a bit more like this:

    @Html.Action("GetCoffee", "MySurface",  yourCoffeeModel)
    

    Likewise, you probably want to be returning a PartialViewResult from the GetCoffee() method - but not a biggie.

    Is there any chance you can post a bit more detail about the main Razor view you have (where you are calling the Html.Action), to try and understand a bit better where you're coming from?

    In the meantime - give that a try and let me know how you get on.

    Cheers,

    Laura

  • Harry Spyrou 212 posts 604 karma points
    Nov 29, 2018 @ 09:48
    Harry Spyrou
    0

    Hi Magnus, you should be able to get content from Umbraco in this case by doing:

    content.GetPropertyValue("propertyAliasHere")
    

    in your constructor, right?

    And then just have the view inherit from your model like:

    @inherits UmbracoViewPage<YourModelHere>
    
Please Sign in or register to post replies

Write your reply to:

Draft