Copied to clipboard

Flag this post as spam?

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


  • Morten Jensen 41 posts 104 karma points
    Mar 17, 2014 @ 16:28
    Morten Jensen
    0

    Best practice for MVC in Umbraco 7

    Hello dear Umbracians

    I have given my self the task of creating a true MVC website in our lovely Umbraco 7. But I keep hitting the wall with some missing best practice and documentation.

    What are the best way of creating a strongly typed MVC setup and when to do what?

    I have read the following articles http://24days.in/umbraco/2013/creating-reusable-code-in-mvc-apps/, http://pwee167.wordpress.com/2014/02/16/umbraco-7-what-ive-learned-so-far-custom-controllers-and-models/ and studied the Hybrid Framework. 

    But these infomations is a mix between Umbraco 6 and 7 which is a bit confusing.

    My frustration is focused on:

    - Should the model have a complete representation of the related document type (no use of GetPropertyValue(propertyName) in the views), or should i only implement new properties and logic?

    - If I have a complete representation of the document type where (controller or model) and how do I best populate the model with the data from the document.

    Thank you for your time

    Jacob

  • xrisdoc 54 posts 102 karma points
    Mar 18, 2014 @ 22:52
    xrisdoc
    0

    Hello,

    Check out the following two blog posts:

    And also this Demo code at https://umbracomvcdemo.codeplex.com/

    I found that my views were starting to contain far too much logic than i'd like. But after reading these posts and reviewing the source code, it really helped me in achieving a more traditional MVC structure.

    The models should reflect the structure of your document types in terms of both properties and inheritance. For example, if you have child document types, the model for this child doc type should inherit from the model that represents the parent document type.

    I think the controllers are the best place to populate the model before passing it to the view. You can populate the properties of the model by obtaining the property values in the same way you normally would (i.e. CurrentPage.GetPropertyValue("pageTitle"))

    Hope this helps.

    Chris

  • Tuan Nguyen 26 posts 166 karma points
    Dec 02, 2016 @ 03:42
    Tuan Nguyen
    0

    Hi Xrisdoc,

    Could you share username and password of umbraco back office help me I can't to access to umbraco backoffice.

    Thanks Tuan Nguyen.

  • xrisdoc 54 posts 102 karma points
    Dec 02, 2016 @ 10:16
    xrisdoc
    0

    Hi Tuan,

    Unfortunately, I do not know the username/password for that demo, it's been a while since I last looked at this and if it is not provided along with the source code, then I can't help with that unfortunately.

    Also, the demo isn't actually mine, I only used it as a reference for developing my own application.

    However, I guess you may be able to change the password for the main admin user using the Umbraco User Service with code like this:

    var userService = global::Umbraco.Core.ApplicationContext.Current.Services.UserService;
    var user = userService.GetByUsername("admin"); // Change admin to whatever the username is.
    userService.SavePassword(user, "Password1!");
    

    Hope this helps.

    Thanks, Chris

  • Tuan Nguyen 26 posts 166 karma points
    Dec 02, 2016 @ 12:15
    Tuan Nguyen
    0

    Thanks Xrisdoc about your post

    That is what i need, i have the another way as link below

    http://jondjones.com/reset-your-umbraco-7-password-manually-via-the-database/

    Have a nice day ^^.

    Thanks Tuan Nguyen.

  • Morten Jensen 41 posts 104 karma points
    Mar 19, 2014 @ 09:31
    Morten Jensen
    0

    Thank you very much.

    This seemed to connect the missing pieces in my head :-)

    I am now happily on the MVC road.

  • svenn 2 posts 22 karma points
    Mar 26, 2014 @ 09:32
    svenn
    0

    Hi 

    Nice articles on using umbraco for pure content display. But what about data input (forms)?

    Do you have any thoughts on this? Or perhaps you want to elaborate a bit on my thoughts?

    In order to have forms you need a SurfaceController to receive the posted data with typed content (view model).
    In order to populate a form with typed content you need a RenderMvcController.
    So in order to have a single contact form (name, email, message) you need 2 controllers (RenderMvc + Surface), 1 view model (inherited from RenderModel), 1 document type (in umbraco) and 1 template (cshtml, also referenced i umbraco).

    Is this the right way to go about this?

    I would love to see a some code or hear some comments on this. Because it seems that we have one controller too many (compared to asp.net mvc)?

    Thank you!

    Svenn

  • Damien Holley 179 posts 540 karma points
    Dec 02, 2016 @ 22:26
    Damien Holley
    0

    I have seen a few examples of using a surface controller to have a partial. Then you use the @Html.Partial("view", new modeltype model) style of code to generate the form.

  • Greg Berlin 818 posts 634 karma points
    Jul 13, 2015 @ 03:37
    Greg Berlin
    0

    Hey all,

    Wow it's been a while since I've done any development on Umbraco, but the time has come for me to play with my favourite CMS again.

    I'd like to start working with MVC, and wondering if the example at https://umbracomvcdemo.codeplex.com/ is still considered "Best Practice", or if there's a better example around that I can use to learn from?

    Cheers Greg

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Jul 22, 2015 @ 13:34
    Alex Skrypnyk
    0

    Hi Greg,

    I think it's not the best practice, it's one of the possible ways. I like to use ZBU Model builder for mapping data from Umbraco to strongly typed models, in this case you don't need controllers with population models from Umbraco like this :

        public ActionResult FrontPage()
        {
            var model = new FrontPageModel();
    
            model.BodyText = CurrentPage.GetPropertyValue<IHtmlString>("bodyText");
    
            return View(model);
        }
    

    Here no logic, just code, I don't know why we need to write it ))

    Thanks, Alex

  • Greg Berlin 818 posts 634 karma points
    Jul 27, 2015 @ 07:51
    Greg Berlin
    0

    Thanks Alex,

    Yeah I actually built my own framework, and shifted all the model population work to the model itself... seems to make more sennse then doing it in the controller.

    I also added some extra levels of inheritance, so i now have a pretty nice framework that i can use to kickstart any project I do... and then for more specific project requirements i'll just inherit from my base models and controllers :)

    MVC rocks!

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jul 27, 2015 @ 08:01
    Jamie Pollock
    1

    Hey Greg,
    Sounds pretty good. Is it similar to Ditto?

    Thanks,
    Jamie

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Jul 27, 2015 @ 07:58
    Alex Skrypnyk
    0

    Hi Greg,

    Interesting to look at your framework )) It would be grat to see it at github or so something like that )

    Thanks

  • Greg Berlin 818 posts 634 karma points
    Jul 28, 2015 @ 00:21
    Greg Berlin
    0

    Sure I'll put it on github when it's ready - it's still a WIP. Watch this space :)

  • Greg Berlin 818 posts 634 karma points
    Jul 28, 2015 @ 00:22
    Greg Berlin
    0

    @Jamie no it's not like Ditto (which looks pretty great by the way - i might integrate that into my framework).. it's more around the controllers and views. Together they'd be pretty awesome I think, thanks for showing me that one :)

  • Greg Berlin 818 posts 634 karma points
    Aug 10, 2015 @ 01:28
    Greg Berlin
    0

    So on further investigation and experimentation, I've decided it's much nicer to move the model population away from the model, but also keep it out of the controller. Bo Mortensen does this in a "Repository" class, which is basically the interface between the Model and the Data Repository. I really like this approach as it keeps all the data-specific code in just one class (the Repository), and decouples the model from the data.

    And if you're super keen, you can use Dependency Injection to decouple the controller from Umbraco too. Probably overkill for your average website, but definitely worthwhile for bigger projects where access to multiple systems may be required (and especially if switching between repositories is a requirement).

    I don't think I could add much to this except post the link: http://24days.in/umbraco/2013/creating-reusable-code-in-mvc-apps/

    Definitely the nicest Umbraco MVC implementation I've seen so far :)

  • Charles Afford 1163 posts 1709 karma points
    Aug 10, 2015 @ 10:25
    Charles Afford
    0

    I would definite recommend DI and GlassMapper for umbraco :)

  • Greg Berlin 818 posts 634 karma points
    Aug 10, 2015 @ 11:51
    Greg Berlin
    1

    Ah yes Glassmapper, i forgot about that. I've worked with it on a Sitecore project before but not with Umbraco.

    Any thoughts on Glassmapper vs DItto?

  • Charles Afford 1163 posts 1709 karma points
    Aug 10, 2015 @ 12:01
    Charles Afford
    0

    GlassMapper hands down. Again not really used it outside of Sitecore and not having TDS is gonna suck but it's really good :D

Please Sign in or register to post replies

Write your reply to:

Draft