Copied to clipboard

Flag this post as spam?

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


  • Niker 21 posts 91 karma points
    Jun 06, 2019 @ 19:24
    Niker
    0

    Umbraco custom viewmodel

    I would like to pass the data to the view, I created a viewmodel inheriting from RenderModel but when I run to the error "Element" Umbraco.Web.Models.RenderModel "does not contain the definition" Topic "

    ViewModel:

    namespace Umbraco12.Models
    {
        public class Home : RenderModel
        {
            public Home(IPublishedContent content, CultureInfo culture) : base(content, culture)
            {
            }
    
            public string Topic { get; set; }
        }
    }
    

    Controller:

    public class HomeController : Umbraco.Web.Mvc.RenderMvcController
        {
            // GET: Home
            public ActionResult Home(RenderModel model)
            {
    
                var home = new Home(model.Content, model.CurrentCulture);
                home.Topic = "aloha";
                //Do some stuff here, then return the base method
                return View("Home", home);
            }
        }
    

    View:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<Home>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = "Master.cshtml";
    }
    <h1>@Umbraco.Field("topic") : @Model.Topic</h1>
    
  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Jun 06, 2019 @ 21:26
    Marc Goodson
    0

    Hi Niker

    At first glance, I wondered if in your view your 'Home' model was the one you have created in the Umbraco12.Models namespace or one generated by ModelsBuilder... as you have a 'using' statement for the auto generated PublishedContentModels, and if your doc type is called Home....

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<Home>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    Try

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<Umbraco12.Models.Home>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    or call your custom ViewModel something like HomeViewModel instead of Home, and that might avoid the clashes, if that is indeed the issue here!

    regards

    marc

  • Niker 21 posts 91 karma points
    Jun 07, 2019 @ 08:24
    Niker
    0

    I tried:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<Umbraco12.Models.Home>
    

    now I have a mistake: You can not use the type "Umbraco12.Models.Home" as a parameter of the type "TContent" in the general type or the "Umbraco.Web.Mvc.UmbracoTemplatePage

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Jun 07, 2019 @ 08:59
    Marc Goodson
    1

    How about

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco12.Models.Home>
    

    ??

    regards

    Marc

  • milkman matty 31 posts 125 karma points
    Sep 24, 2020 @ 23:09
    milkman matty
    0

    Marc, you're a beautiful man!

  • Niker 21 posts 91 karma points
    Jun 07, 2019 @ 10:46
    Niker
    0

    This working, thanks.

Please Sign in or register to post replies

Write your reply to:

Draft