locked
Declare and use variables using my own class in RAZOR [ ASP.NET MVC ] 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:37 AM

All replies

  • User-821857111 posted

    You can't access const or static members via in instance of the class. Use the type name instead:

    <td><a href="/Admin/@MyNameSpace.BL.TXT_and_ROUTE.Route.STG_Route.INDEX"><button type="button" class="btn bg-blue btn-block btn-sm waves-effect">xxx</button></a></td>
    Thursday, June 11, 2020 5:19 PM
  • User1686398519 posted

    Hi,  MBARK

    You can modify your page code like this.

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

    Here is the result.

     
    Best Regards,

    YihuiSun

    Friday, June 12, 2020 3:24 AM