I have seen this related question Creating and Simple Class and Calling a Method from a cshtml File. How can we do that without using the App_Code folder?
In my project's root, I have a Something.cs class:
public class Something
{
public static void DoIt()
{
}
}
In my project's root, I also have index.cshtml with the following code:
@Something.DoIt()
The name Something does not exist in the current context of index.cshtml.
Have you tried to refer the projects library within the cshtml File
@using ProjectName;
Or refer to the object like this @ProjectName.Something.DoIt()
If you are accessing it on top of .cshtml or inside any HTML string template like for loop then
@{
MvcDemo.Something.DoIt();
}
Inside BeginForm or the other then
@using (Html.BeginForm())
{
MvcDemo.Something.DoIt();
}
Please Note MvcDemo is the namespace and if you direclty write @MvcDemo.Something.DoIt() you may get error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With