Copied to clipboard

Flag this post as spam?

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


  • Advance Ngwarati 6 posts 96 karma points
    Feb 07, 2019 @ 16:15
    Advance Ngwarati
    0

    Understanding How ViewModel Works

    Hello there,

    I have an existing project which is using this concept@inherits UmbracoViewPage<LandingViewModel> in Views. My problem now is I'm failing to understand where exactly LandingViewPage is located, where it comes from, and how this concept really works. Forgive me I'm really new to ASP.NET and Umbraco.

    Thanks in advance.

  • Louis Jackson-Rees 91 posts 317 karma points c-trib
    Feb 07, 2019 @ 16:47
    Louis Jackson-Rees
    101

    Hello,

    Generally this follows this type of structure with MVC and Umbraco:

    your model would be: YourDoctypeNameViewModel.cs your controller would be: YourDoctypeNameController.cs your view would be: YourDoctypeName.cshtml

    Your view file will then inherit from UmbracoViewPage<YourDoctypeNameViewModel>

    Umbraco uses its route-hijacking explained here: https://our.umbraco.com/documentation/reference/routing/custom-controllers

    to know that for your doctype it should use the YourDoctypeNameController.cs controller to render that particular route (this is all based on the naming, as it is the same as your doctype) so then in your controller you will need to return your view passing the YourDoctypeNameViewModel.cs model data to it

    alternatively if you want to access just page data and need no custom model properties or logic in the controller, you could use models builder which allows you to skip the controller step

  • Advance Ngwarati 6 posts 96 karma points
    Feb 08, 2019 @ 11:12
    Advance Ngwarati
    0

    Hi Louis,

    Thank you for very much, now I have light. Do you have any specific posts or tutorials which might help me to understand models builder. It seems like this concept was used on this project I'm working on.

    I managed to follow the steps you mention above using ModelsBuilder module, but I'm still getting the error "The type or namespace name 'LandingViewModel' could not be found (are you missing a using directive or an assembly reference?)". Any help?

  • Louis Jackson-Rees 91 posts 317 karma points c-trib
    Feb 08, 2019 @ 14:33
    Louis Jackson-Rees
    0

    I would recommend this walkthrough: https://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/

    and the documentation: https://our.umbraco.com/documentation/Reference/Templating/Modelsbuilder/Introduction

    So if you are using models builder you will probably want to change you view to inherit

    @inherits UmbracoViewPage<YourModelsBuilderGeneratedClassName>
    
  • Advance Ngwarati 6 posts 96 karma points
    Feb 08, 2019 @ 15:05
    Advance Ngwarati
    0

    Thank you.

    You saved my life!

Please Sign in or register to post replies

Write your reply to:

Draft