James Kingston
James Kingston

Reputation: 7

How can I fix "is a variable but is used like a type" error in my asp.net mvc Webapp?

When I try to create the following Class on my Main_Controller, I get the error

"CS0118 'prjname' is a variable but is used like a type"

Here is my code

public ActionResult AufgabenDetails(int id)
        {
            var prjname = new prjname { Title = "Album " + id };
            return View(prjname);
        }

Can anyone help me?

Upvotes: 1

Views: 2682

Answers (2)

Inbar Barkai
Inbar Barkai

Reputation: 302

The class name and variable name are the same, change it:

public ActionResult AufgabenDetails(int id)
        {
            var p= new prjname { Title = "Album " + id };
            return View(p);
        }

Upvotes: -1

Mostafiz
Mostafiz

Reputation: 7352

Here prjname is a anonymous type variable but you need to pass a specific model type in View, so create a model class for it and use same model type in View page

class SampleModel {
  public string Title {get;set;};
}

public ActionResult AufgabenDetails(int id)
{
      SampleModel  prjname = new SampleModel { Title = "Album " + id };
      return View(prjname);
}

Upvotes: 2

Related Questions