Copied to clipboard

Flag this post as spam?

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


  • J 445 posts 862 karma points
    Oct 13, 2020 @ 11:08
    J
    0

    How to see Views in Umbraco?

    I installed Umbraco 7 inside Visual Studio empty project as per the documents on this site. Started the site and installed a starter kit. All is working as i would expect.

    I now create a Model and then create a View within VS. The View has been saved to the default location (Site\Views\CONTROLLER\file.cshtml).

    The next step is to log onto the site through a browser and navigated to Settings/Developers and other locations but i cant seem to find the Views i created?

    I can see all the Partial Views but not the ones outside of that folder. How do i access/view them within Visual Studio?

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Oct 13, 2020 @ 11:29
    Steve Morgan
    1

    Hi,

    Have you created a document type? This will generate a view for you (hidden in VS so you have to show / hide files). Then you build your models using models builder.

    It sounds like you've come from a C# background and want to go code first - building models and controllers - that's not how Umbraco works.

    I'd suggest you go through the basic tutorial - there will be an "ahh.. that's how it works" moment.

    https://our.umbraco.com/documentation/tutorials/Creating-Basic-Site/

    Then look at the models builder.

    If you are a c# dev then look at custom controllers and you should see how to do everything you need (and are used to!).

    Steve

  • J 445 posts 862 karma points
    Oct 13, 2020 @ 12:18
    J
    0

    Hi

    Yes you're correct i usually create Models, Controllers and separate my projects into dlls, using Entity Framework, Unit of work, Unit testing, Dependency Injection. Just mentioning these as i would like to introduce these into Umbraco (hopefully??).

    Were starting to go into Umbraco a little more as most things are done relatively quickly compared to certain tasks.

    I will go through the link you posted but if there is any other material i could utilise it would be great if you could link anything. We also have a sub to Pluralsight so if theres anything on there i could use again be great for any pointers.

    I didnt create a Document Type, all i did was create a Model and then generated a View which showed up in VS but not in the Umbraco site i created. I then added a Template and assigned

    @Html.Action("Index", "ContactForm")
    

    Inside the template to display the View. This displayed everything i required but the reason for my question was if i needed another developer who may not have VS (and access to only the Umbraco site as in backend) how could they see this View and make any changes?

    Thanks

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Oct 13, 2020 @ 13:28
    Steve Morgan
    1

    Hi,

    You really are best to go through the tutorial - don't try to fight the Umbraco routing.

    Doc types are vital to Umbraco - you need these to provide editors with a useable site. Try to park your MVC brain - go slow and go through the tutorials!

    Creating the doc type will create a view that other can see in the Umbraco backend. What you created won't because it's not in the root /Views/ folder.

    I suggest you then look at the Models builder - it takes your doc types and makes classes for you. You won't need to do too much coding.

    For forms look at Surface Controllers.

  • J 445 posts 862 karma points
    Oct 13, 2020 @ 13:56
    J
    0

    Hi Steve

    Thanks for that and i will go through those tutorials shortly.

    So just to make sure I've understood you correctly as i went through a few pages before i felt i may have misled you.

    If i wanted to create a contact form, I created a Doc type (Name, Email and Message). This in turn created the Template under the template section with the default Razor code i.e.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Contactable>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = null;
    }
    

    Once i have this template, i would still need to carry out some other functionality i.e. send and email and maybe record it in a database of the details entered. So its at this stage i would imagine i need to do some coding - does this sound like were attacking the same goal post? I assume i would then create a controller to for this Template but i wont go there just yet as i assume this is whats covered in the tutorials :-)

    Thanks again for your help

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Oct 13, 2020 @ 14:13
    Steve Morgan
    1

    As per my post.

    For forms - look at Surface Controllers. And no - your model for a user submission would be a more classic c# class.

  • J 445 posts 862 karma points
    Oct 13, 2020 @ 14:28
    J
    0

    Ok a little confused :-( but does this look correct as per my understanding

    I will take a look at Surface Controllers for version 7 of Umbraco (https://our.umbraco.com/Documentation/Reference/Routing/surface-controllers) (and the tutorials you mentioned above).

    Leave the Doc type in place and use this Doc Type to build the model so it can be used in a class to send an email.

    Finally learn on modelBuilders for Umbraco?

    If this sounds like im on the right lines then i can mark this as answered.

    Many thanks

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Oct 13, 2020 @ 14:44
    Steve Morgan
    100

    Hi,

    Nearly the right lines...

    At first it sounded like you were trying to output content. When you made it clear you were doing a contact form then surface controllers along with custom models (nothing to do with the document type) are the way to do this.

    You will likely need a contact us doc type so you can add the partial to render the form but it won't need to add to this doc type the fields that you're planning to capture on the form - these will come from a standard model as per the documentation for surface controllers.

    Steve

  • J 445 posts 862 karma points
    Oct 13, 2020 @ 15:07
    J
    0

    At first it sounded like you were trying to output content. When you made it clear you were doing a contact form then surface controllers along with custom models (nothing to do with the document type) are the way to do this.

    Ok so i will look into Surface Controller but my obstacle was with displaying the View within Umbraco. So this would mean theres no way to display the View in Umbraco administrative section? The only reason i want to display it, is so if there were changes needed i.e. adding some static text on the View it can be done by someone at that level in the company.

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Oct 14, 2020 @ 07:34
    Steve Morgan
    1

    Hi,

    If you want additional Umbraco CMSed content within the partial that renders the form (titles, intro text, error codes etc) there's a slight problem as you need to pass the form model (form data) into the partial to render the form and later handle the post and there isn't the chance to pass the Umbraco doc type model in as well.

    My usual workaround is in the Surface Controller to populate a ViewModel class with the labels and error text. So in the RenderForm method do something like:

            var viewData = new ContactUsViewModel
            {
                PhoneNumber = siteSettings.PhoneNumber,
                PhoneNumberUnformatted = siteSettings.PhoneNumberUnformatted,
                Email = siteSettings.Email,
                NameLabel = CurrentPage.GetPropertyValue<string>("nameLabel"),
                EmailLabel = CurrentPage.GetPropertyValue<string>("emailLabel"),
                QuestionLabel = CurrentPage.GetPropertyValue<string>("questionLabel"),
                SubjectLabel = CurrentPage.GetPropertyValue<string>("subjectLabel"),
                MessageLabel = CurrentPage.GetPropertyValue<string>("messageLabel"),
            };
    
            ViewData["ContactUsViewData"] = viewData;
    

    Then create your form model as per the documentation and return that in to the partial view to render your form.

        ContactSubmission model = new ContactSubmission();
    // prepopulate any form data here
         return PartialView("ContactUs/ContactUs", model);
    

    Now in your partial view that renders the form you're still just expecting the form data model:

     @model YourNamespace.Models.ContactSubmissions.ContactSubmission
         @{
            var viewData = ViewData["ContactUsViewData"] as YourNamespace.Models.ContactSubmissions.ContactUsViewModel;
        }
            ... @* form render here  use model for the form fields and viewData for labels / titles / error messages etc *@ 
    

    Does that make sense - you're essentially building a second model, storing it to ViewData so that you can get it again in the partial.

    In some situations (AJAX) you might need to add the content node's ID / key to the form data model so that you know where the form has come from so you can get the the correct page's view data. Here you just add it on the render method and then you have it on the post if you get the node using standard Umbraco methods.

    HTH

    Steve

  • J 445 posts 862 karma points
    Oct 14, 2020 @ 11:05
    J
    0

    Hi

    I think that makes sense so i will go through the links you posted to follow some steps on this.

    One other way i found was to cut the folder from the Views VS location and then paste it within Partial Views which resulted in the file appearing within Umbraco

    Thanks again

Please Sign in or register to post replies

Write your reply to:

Draft