locked
Declare and use variables using my own class in RAZOR RRS feed

  • Question

  • User1236783433 posted

    Inside a view I'm trying to use variable created from my own class,

    My class code :

    public class STG_Route
    {
        public const string INDEX = "STG";
        public const string ADD = "STG/Add";
        public const string SHOW = "STG/Show";
        public const string PROFILE = "STG/{CODE}";
    }

    My view code :

        @{ 
        MyNameSpace.BL.TXT_and_ROUTE.Route.STG_Route Route = new MyNameSpace.BL.TXT_and_ROUTE.Route.STG_Route();
    }
    <td><a href="/Admin/@Route.INDEX"><button type="button" class="btn bg-blue btn-block btn-sm waves-effect">xxx</button></a></td>

    But this gave me errors, please any help ?

    Thursday, June 11, 2020 11:43 AM

Answers

  • User475983607 posted

    Constants by definition are not instance members.  Constants are accessed directly from the class.

    Add a using statement to the top of the View. 

    @using MyNameSpace.BL.TXT_and_ROUTE.Route

    Then access the constant directly from the STG_Route class.  I hope you don;t mind that I took the liberty of fixing the invalid HTML.

    <a href="/Admin/@STG_Route.INDEX" class="btn bg-blue btn-block btn-sm waves-effect">xxx</a>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 11, 2020 12:20 PM

All replies

  • User475983607 posted

    Constants by definition are not instance members.  Constants are accessed directly from the class.

    Add a using statement to the top of the View. 

    @using MyNameSpace.BL.TXT_and_ROUTE.Route

    Then access the constant directly from the STG_Route class.  I hope you don;t mind that I took the liberty of fixing the invalid HTML.

    <a href="/Admin/@STG_Route.INDEX" class="btn bg-blue btn-block btn-sm waves-effect">xxx</a>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 11, 2020 12:20 PM
  • User1236783433 posted

    Massive thanks bro, this help me

    Thursday, June 11, 2020 5:32 PM