Neo
Neo

Reputation: 16219

unable to get property reference from other C# project in mvc application

I have created one c# library project. I have created another MVC application added C# project reference into mvc application.

Now on View I want to access that property which is there inside C# class library project but I'm unable to do so why?

C# project code

namespace Helper.BaseClasses
{
    public abstract class BaseView<T> : WebViewPage<T>
    {
        private TestClass localizer;

        public override void InitHelpers()
        {
            base.InitHelpers();
            localizer = new TestClass();
        }

        public TestClass Lang
        {
            get { return localizer; }
        }
    }
}


public class TestClass 
{
 public string TestMethod(string input)
        {
//some code
}
}

Mvc application added reference dll of c# Index.cshtml

<h4>@Lang.TestMethod("Customer agreements", "")</h4>

When I tried to use TestMethod in Index.cshtml it is given me error

The name TestMethod does not exist in current context

after adding C# project dll reference why I'm getting this error :(

Upvotes: 0

Views: 143

Answers (1)

Vadim Pashkov
Vadim Pashkov

Reputation: 527

Have you set "pageBaseType" in web.config? If not, try to set it to your base class. To do that you need to find "pages" tag in ~/Views/web.config. It would be something like:

<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="WebApplication1" />
  </namespaces>
</pages>

You need to change it to:

<pages pageBaseType="Helper.BaseClasses.BaseView">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="WebApplication1" />
  </namespaces>
</pages>

And rebuild solution. Hope this helps.

Upvotes: 1

Related Questions