Copied to clipboard

Flag this post as spam?

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


  • Christian Koch 20 posts 90 karma points
    Apr 05, 2021 @ 02:01
    Christian Koch
    0

    Hallo everybody,

    I use a specific data model (classes) that I use to fill with Json-Data (Deserialize over Newtonsoft).

    At the moment I define this classes in the template with

    @function{
    
    public class Product
        {
            public string ProductID { get; set; }
            public string ProductName { get; set; }
            public string ProductCategory { get; set; }
    } 
    
    }
    

    I would like to put this model somewhere in the project. So I created a folder "Models" in the root folder of Umbraco.

    I tried to use that model by putting the folloing using-directive in the template.

    @using umbraco.Models;
    

    This does not seem to work. What do I have to do?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Apr 05, 2021 @ 07:02
    Marc Goodson
    0

    Hi Christian

    From your explanation, I'm not sure how you are working with Umbraco.

    If you have a Visual Studio solution for your project, then you can add a c# class to a folder in your web application solution, and this will automatically generate a namespace for your new class, when the solution is built into a dll, this will include the class and you can use the 'namespace' in the using statement to refer to it in your views/templates.

    namespace MyProject.Models {
    public class Product
        {
            public string ProductID { get; set; }
            public string ProductName { get; set; }
            public string ProductCategory { get; set; }
    } 
    
    }
    

    and then:

    @using MyProject.Models
    

    But because you've mentioned creating it in the Umbraco folder, and refer to it as umbraco.Models... it makes me think you might not be using Visual Studio?

    if not then your class won't be compiled...

    ... but you can have it compile on the fly, if in the root of your project (not the umbraco folder) you have a special called app_code - and anything you put in here will be compiled when your application starts.

    So you could then create a product.cs file inside the app_code folder, and put your custom product class implementation in there... without using Visual Studio or similar to do the compilation.

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft