Copied to clipboard

Flag this post as spam?

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


  • Josip 195 posts 662 karma points c-trib
    May 16, 2019 @ 10:21
    Josip
    0

    Can i use model generated in AppData mode on another template

    Hi,

    Can I use models in AppData mode generated by models builder in another Document Type template?

    What i am trying to do:

    I have node News and his child News Item , where i want to create and update news programmatically but from another templates called Create News and Update News.

    I have tried to inherit News Item model in Create News and Update news templates like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.NewsItem>
    

    and like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<NewsItem>
    

    But its not working. What would be the best way to do this?

  • Josip 195 posts 662 karma points c-trib
    May 16, 2019 @ 12:00
    Josip
    0

    Would it be good solution to create 3 templates from same document type maybe?

    I dont know what is the best practice to do it.

  • Bryna 74 posts 260 karma points
    May 20, 2019 @ 18:45
    Bryna
    0

    I'm not sure if I understand what you are wanting to do. It sounds like you want to present a news story in one of three formats. In this case you would have 3 views(templates) for each NewsItem. If you are wanting the newsitem to display in one of three ways, then you will need a Property on your newsitem to allow your code to know which way to display the newsitem. A bit of psuedocode below. Assume that News is the parent Document Type and NewsItem is the child. Your news.cshtml file might look like:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.News>
    @using ContentModels = Umbraco.Web.PublishedModels;
    @{
        //Partial view that can be palced on any skin.
        Layout = null;
        IPublishedContent theNews = GetNews();
    }
    <h1>News</h1>
    @foreach(News itm in theNews)
    {
      switch(itm.NewsFormat)
      {
        case "Format1":
          @Html.Partial("~/Views/Format1.cshtml", itm)
          break;
        case "Format2":
          @Html.Partial("~/Views/Format2.cshtml", itm)
          break;
        case "Format3":
         @Html.Partial("~/Views/Format3.cshtml", itm)
          break;
        default:
         <div>No format Available for news</div>
          break;
      }
    }
    

    What the above code does is basically leverages the power of MVC partial views so that your data is separate from how it is presented. I believe that the following link kind of explains a bit of pre-requisite knowledge:https://our.umbraco.com/documentation/Tutorials/Creating-Basic-Site/Articles-Parent-and-Article-Items/

Please Sign in or register to post replies

Write your reply to:

Draft